Blog

Save Social Media Share Count During Domain Change

social-media-proofThere are several situations that could result in lost social share counts:

  • Dissociated short links
  • Domain name change
  • URL structure change

This solution for restoring or preserving social media share counters is iterative. It allows you to set a specific share url, depending on the post ID. If a post ID is not flagged out, the post share data will be populated by the permalink. You could set it up in your functions.php file, a custom plugin, or on a page template ( like single.php ).

I created this solution recently, when, after deploying a fresh rebuild of an old site, all of the social media share counters were lost. In my case, the social media share counts were associated with shortlinks generated by bitly that became disassociated with the website upon the change (despite that the permalink, database entry, GUI, WordPress version and settings were identical).

My research turned up little information on how to handle the situation. One post stood out – this one, by Dave Ward – and inspired the solution that I devised.

<div class="share-before share-filled share-small">

<?php

$sp_id = get_the_ID();          // GET THE POST ID, WE'LL SET THE DATA URL BY ID
$sp_post_title = get_the_title();         // PULL THE TITLE VIA THIS STRING

if ( $sp_id == 2773 ) {          // FLAG DOWN A POST BY ID
$sp_share_url = 'http://domain.com/post3';         // SPECIFY THE SHARE URL

// THE SOCIAL SHARE STRUCTURE

} elseif ( $sp_id == 433 ) {         // FLAG DOWN A POST BY ID
$sp_share_url = 'http://domain.com/post2';         // SPECIFY THE SHARE URL

// THE SOCIAL SHARE STRUCTURE

} else {          // RUN SHARE TECHNIQUE FOR NEW, NORMAL POST
$jwd_post_id = get_the_ID();
$jwd_post_url = get_permalink();
$jwd_post_title = get_the_title();

// THE SOCIAL SHARE STRUCTURE

}

?>

</div>

Any “Social Share  Structure” should be able to be adapted for use with the query above. I recently decided that I love Daniel Setzermann’s Custom Social Media Buttons for WordPress. So, I’m going to example the implementation with that markup.

In share.php, Daniel provides markup for the share tools. Let’s just look at Facebook:

<div class="facebook sharrre"         // CLASSES FOR GENERAL STYLE

id="facebook-<?php the_ID(); ?>"         // ID FOR UNIQUE STYLE

data-url="<?php the_permalink(); ?>"         // SHARE URL

data-text="<?php the_title(); ?>"         // SHARE CONTENT

data-title="Like">         // SPECIFIES WHAT KIND OF INFORMATION FACEBOOK SHOULD SAVE

The most important thing here is the DATA-URL. When someone clicks the LIKE button, Facebook adds a digit to the like count of the DATA-URL. You can see it live by using “https://graph.facebook.com/?id=domain.com” As in, https://graph.facebook.com/?id=http://bit.ly/jryvenwd. Or – for more information, something like devised here.

What we want to do is specify the DATA-URL rather than allowing THE_PERMALINK();  to set the DATA-URL. Here’s how I set up the code for Facebook to fit within the query I showed you above:

[PHP]

echo

‘;       // CLOSE THE DATA-TEXT, ADD THE DATA-TITLE, CLOSE THE DIV

[/PHP]

Putting it all together, my function looked like this:

 

[PHP]

‘; //OPEN SHARE ITEMS CONTAINER

$sp_id = get_the_ID(); // GET THE POST ID, WE’LL SET THE DATA URL BY ID
$sp_post_title = get_the_title(); // PULL THE TITLE VIA THIS STRING

if ( $sp_id == 1 ) { // FLAG DOWN A POST BY ID
$sp_share_url = ‘http://bit.ly/jryvenWD’; // SPECIFY THE SHARE URL
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;

} elseif ( $sp_id == 2 ) { // FLAG DOWN A POST BY ID
$sp_share_url = ‘http://bit.ly/jryvenWD2’; // SPECIFY THE SHARE URL
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;

} else { // RUN SHARE TECHNIQUE FOR NEW, NORMAL POST
$jwd_post_id = get_the_ID();
$jwd_post_url = get_permalink();
$jwd_post_title = get_the_title();
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
echo ‘

‘;
}

echo ‘

‘; // CLOSES SHARE ITEMS CONTAINER
?>

[/PHP]

What’s Next

Our Work

Check out some recent work we’re proud of.

Our Blog

Read our latest ideas about tech, features, messaging, and more.

Get in Touch

Want to discuss a project? Hit us up, we’d love to chat!