Skip to content

Instantly share code, notes, and snippets.

@jeremychurch
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save jeremychurch/78c40a13a52368495784 to your computer and use it in GitHub Desktop.

Select an option

Save jeremychurch/78c40a13a52368495784 to your computer and use it in GitHub Desktop.
Maintain social button counts for WordPress posts
<!-- If you switch to https, you lose the count that appears next to Twitter and Facebook buttons.
Here's a snippet to maintain previous counts for WordPress posts published before implementing https. -->
<?php
// Change the date to the day you switched to https
// Insert the following within the WordPress theme file single.php
$url_change_date = strtotime("8/16/2014");
$post_date = strtotime(get_the_date());
$sharing_url = get_permalink();
if ($post_date < $url_change_date) {
$url_prefix = "http://";
$sharing_url = str_replace("https://",
"http://",
$sharing_url);
}
?>
<!-- For Twitter, find the following line and add the data-url attr -->
<a href="https://twitter.com/share" data-url="<?php echo $sharing_url; ?>" class="twitter-share-button">Tweet</a>
<!-- For Facebook, find the following line and add the data-href attr -->
<div class="fb-like" data-href="<?php echo $sharing_url; ?>" data-layout="button_count" data-action="like" data-show-faces="false" data-share="false"></div>
<!-- For Google+, do nothing ... they did it right.
The count will persist regardless of which protocol is used. -->
<!-- Modified slightly from the original source:
http://encosia.com/preserving-social-sharing-counters-through-a-url-change/ -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment