Lutfi Torla.com

Keeping ShareThis & Digg Digg social counts after a WordPress Permalink change

I recently changed my theme. While changing, I figured that I would change my WordPress permalink structure as well. Apparently it’s good SEO to have shorter links. My previous link structure was www.lutfitorla.com/[year]/[month]/[post-name]. At the same time, I wanted to stop having the date of publishing in the link. Apparently this stops some people from clicking on your link if they think it’s too old. Well, most of my writing isn’t really bound to current news, so I don’t appreciate that effect.

So I updated my theme and at the same time changed my permalink structure. But then…

…my social counts disappeared!

How can I keep them?

I put up facebook share buttons and tweet buttons on my posts (as you can probably see). I hated having to start from scratch. So I googled how to keep my social shares. Coz if there’s one thing I’ve realized by now, if I’m trying to do something, there are others trying to do it too.

Well, guess what? There are many people who are trying to figure out how to keep their social shares, but only ONE!!!! had any sort of solution! Dave Ward is his name and he shared his solution over on encosia.com.

Credit to Dave Ward of Encosia: http://encosia.com/preserving-social-sharing-counters-through-a-url-change/

So I studied what he did and it turns out that the way ShareThis and other social networks keep track of the count is by checking your URL address. So the moment I changed the permalink structure of my website, I also changed the web address. This meant that ShareThis couldn’t know that this was the same web page!

So, what Dave Ward did was to hack the address that the social count button sent back to his social network (delicious & twitter). I understood the general idea of what he did, but I still wasn’t sure I could pull it off myself.

I first did the necessary steps though:

RedirectMatch 301 /d{4}/d{2}/(.*) http://www.lutfitorla.com/post/$1

First I redirected anyone going to the old page so that they would end up at the new address. This includes search engines and also anyone who comes through my old social shares.

$url_change_date = strtotime("7/18/2012");
$post_date = strtotime(get_the_date());
$sharing_url = get_permalink();
if ($post_date < $url_change_date) {
$url_date_prefix = "/" . date("Y", $post_date) .
"/" . date("m", $post_date);
$sharing_url = str_replace("lutfitorla.com/post", "lutfitorla.com" . $url_date_prefix, $sharing_url);
}

Next, I use Dave’s code and set the last date that I was using the old permalink structure as well as what the old permalink structure was. The new permalink structure is then replaced with the old using strreplace() [short for string replace]. This is so that I have the old address to send back to the social network. (Believe it or not, I got stumped on this part of the code for an hour because I had it in my head that I needed to change the old permalink into the new instead of the other way around).

All throughout, I’ve highlighted the parts of the code that you should change and personalize to suit your own website.

This part was simple enough. I now had to figure out which part in that huge plugin needed to be changed. I had an idea though. Going into the admin area, I saw that the ShareThis plugin could output some html code so that you could show the buttons anywhere. In that html code, they used the wordpress the_permalink() function. This function gives the link of the current page you’re on. Ok! Maybe this is what they used!

If it’s true, I just have to go into the plugin and replace the_permalink() with $sharing_url.

So, in the admin dashboard, I went to the right menu bar and went to Plugins > Editor.

At the top right, there’s a dropdown box, I used it to go to ShareThis.

The plugin has many files the one we want is share-this/sharethis.php. It should already be automatically selected.

Now I had the code in front of me. It seemed hard to work here though, so I copy-pasted all the code into Notepad++ and worked from there.

I first put the code I have above into the top of the file (but after the <?php tag) and started searching for and replacing all the pieces of code called the_permalink(). copy-pasting my results into the online editor and updating it, I went to my webpage and refreshed…

…still zero. Dammit.

I reset everything back to original and now started searching for the parameter st_url. This is the parameter used in the ShareThis html code I mentioned above to show the URL address. I found the same bunch of code from last time.

Finally, I realized that it was actually the code slightly above it. Search for st_makeEntries(). Then replace the code with the following:


function st_makeEntries(){
global $post;
//$st_json='{"type":"vcount","services":"sharethis,facebook,twitter,email"}';
$url_change_date = strtotime("7/18/2012");
$post_date = strtotime(get_the_date());
$sharing_url = get_permalink();
if ($post_date < $url_change_date) {
$url_date_prefix = "/" . date("Y", $post_date) .
"/" . date("m", $post_date);
$sharing_url = str_replace("lutfitorla.com/post", "www.lutfitorla.com" . $url_date_prefix, $sharing_url);}
$out="";
$widget=get_option('st_widget');
$tags=get_option('st_tags');
if(!empty($widget)){
if(preg_match('/buttons.js/',$widget)){
if(!empty($tags)){
$tags=preg_replace("/\'/","'", $tags);
$tags=preg_replace("/<?php the_permalink(); ?>/","$sharing_url", $tags);

I’m sure you can figure out what to replace. Just replace it from the function st_makeEntries() declaration until the $tags=preg_replace 2nd line. I’ve highlighted the changes in blue if you’d like to do it manually. Again, replace the date and the website address with your own.

Moving to diggdigg

I realized that I’d been stuck on ShareThis for too long. Their buttons were stuck in their system and I wanted the freedom to move around and change to lighter/faster options. So I tried changing to diggdigg. Turns out that keeping most of my social shares was easier than before.

Editing the diggdigg plugin showed that in the first 30 lines the url for sharing was defined in the function dd_hook_wp_content(). Even better, it took the post’s link and defined it to $postlink. So I just took the script I ran before and changed the final output to $postlink. Then I deleted the line they had


function dd_hook_wp_content($content = ''){
if(dd_isThisPageExcluded($content)==true){
return $content;}

$url_change_date = strtotime("7/18/2012");
$post_date = strtotime(get_the_date());
$sharing_url = get_permalink();
if ($post_date < $url_change_date) { $url_date_prefix = "/" . date("Y", $post_date) . "/" . date("m", $post_date);

$postlink = str_replace("lutfitorla.com/post", "www.lutfitorla.com" . $url_date_prefix, $sharing_url); }
global $wp_query; $post = $wp_query->post; //get post content
$id = $post->ID; //get post id

$postlink = get_permalink($id); //get post link
$commentcount = $post->comment_count; //get post comment count
$title = trim($post->post_title); // get post title
$link = explode(DD_DASH,$postlink); //split the link with '#', for comment link
$url = $link[0];

The green parts are from the original code we used before. The blue part is the small change you need to make. The red part is what you have to delete.

I managed to fix it up in less than 2 minutes. I hope this means that it’s also easy to keep hacked when I update it later. Unfortunately, I lost a great many of my facebook shares and tweets because ShareThis uses their own special implementation. Somehow it doesn’t translate back when I use the more common facebook like to share posts (it should transfer back).

Conclusion

Well, there you go.

It took me ages to figure this out. I hope that if you’re using WordPress like me, this will help you when you want to change your permalink structure but still keep your social counts.

 

13 thoughts on “Keeping ShareThis & Digg Digg social counts after a WordPress Permalink change

  1. L

    Hello, I’m trying to do work through this currently on my WordPress website. However, I currently am using Postname as the Permalink Structure. But I’m having a hard time setting up the redirect code and sharethis.php portion. Any advice?

    1. Lutfi Torla Post author

      Hi there L,
      I’m afraid I’m not that good in coding either. I pretty much just copied Dave Ward’s method every step.

      If I read you right though, you’re not sure where to do the redirect? You’ll have to go to your webhost and look for a file called “.htaccess”.

      Good luck!

  2. Blake Belding

    Thanks 🙂

    Using share-this and recently changed my permalinks. I lost all likes. I changed the permalinks from
    old permalink /%year%/%monthnum%/%day%/%postname%/ to new permalink /%category%/%postname%/.

    I could not figure out how to get the category using the str_replace command.

    Got it:

    This line was the bugger. Note the (‘Catagories’):

    $sharing_url = str_replace(“currentinspiration.com” . (‘Categories’), “currentinspiration.com” . $url_date_prefix, $sharing_url);}

    see code below that goes in share-this.php (you describe the location above).

    function st_makeEntries(){
    global $post;
    //$st_json='{“type”:”vcount”,”services”:”sharethis,facebook,twitter,email”}’;
    $url_change_date = strtotime(“2/24/2013″);
    $post_date = strtotime(get_the_date());
    $sharing_url = get_permalink();
    if ($post_date < $url_change_date) {
    $url_date_prefix = "/" . date("Y", $post_date) . "/" . date("m", $post_date) . "/" . date("d", $post_date);

    $sharing_url = str_replace("currentinspiration.com" . ('Categories'), "currentinspiration.com" . $url_date_prefix, $sharing_url);}
    $out="";
    $widget=get_option('st_widget');
    $tags=get_option('st_tags');
    if(!empty($widget)){
    if(preg_match('/buttons.js/',$widget)){
    if(!empty($tags)){
    $tags=preg_replace("/\'/","'", $tags);
    $tags=preg_replace("//”,”$sharing_url”, $tags);

    1. Lutfi Torla Post author

      Hi Blake,
      I’m not really that amazing at code myself, so I’m glad your managed to figure it out!

      Thanks for posting your solution here too. I’m sure it’ll help others when they pass by.

  3. Mark

    Hi, thanks for taking it this far 🙂
    I’m in a similar situation – wanting to move permalinks from the default structure to /post-name structure.. In the comments section of Dave’s original post a found a similar quest and tried adapting your Digg-Digg script with that, only it doesn’t work :-/
    Do you have any ideas about how to work this? I’ll paste the code in here (the domain names are from the comment mentioned). Digg-Digg returns a fatal error regarding line 43, i.e. $sharing_url = get_permalink();

    $url_change_id = 4205;
    $post_id = get_the_ID();

    $sharing_url = get_permalink();

    if ($post_id < $url_change_id) {
    $url_id_postfix = "/?p=" . $post_id;

    $sharing_url = str_replace("://foodwithlegs.com",
    "://foodwithlegs" . $url_id_postfix,
    $sharing_url);
    }

    1. Mark

      Hm, ok.. turns out I just had to get rid of all the spaces between the lines to make the script actually work. When I did this, the script does run, but Digg-Digg only reflects my old Pocket counts – not Twitter, FB, Linkedin, Google+?? I did not go with the htaccess 301 redirect (yet) as this should not affect the actual Digg-Digg count as far as I can see.. Any ideas?

    1. Kissy Denise

      NEVERMIND.. It works perfectly.. I changed my permalinks a bit too much and got confused.. Between you and BLAKE’S coding, it worked perfectly!! Thumbs Up. You need a donate button on this page.

Leave a Reply