When you change the domain name of your blog, there is an unfortunate and annoying side effect…

If you use any social media sharing buttons, then they will all now show ZERO.

Any social proof that goes along with having retweets, Facebook shares or +1’s is now gone and you feel like you need to start over in that regard.

But, there is a fairly easy solution to this. It requires a little bit of PHP code, but I’ll spare you the time and simply share it with you. 🙂

First Off, Why It Happens

The way these social media sites work, everything is based off the URL being shared. And, when it goes to fetch the number of shares for a URL, it does it based on THAT URL.

So, to illustrate, let’s take this site’s about page and assume it was going to be shared on Twitter. Before my recent domain switch, the URL would have been:

http://www.davidrisley.com/about/

Afterward, the URL was:

https://www.blogmarketingacademy.com/about/

The social buttons are out there looking for the first URL, because that is the one that got shared. Now that the second URL is in effect, when it goes out to look for any existing retweets, it will of course find none.

The Solution

The solution, then, should be pretty apparent…

Have those social media sharing buttons continue to look for the OLD URL – even though you’ve changed your domain. This will keep those social buttons from showing zero’s (assuming, of course, that you actually got some shares before you changed your domain).

You don’t necessarily need to do this little hack for all of your posts, however. You only need to do this for the posts published BEFORE you changed your domain. Everything you write after the change should continue to share out the new (and current) domain name.

The good thing is that this is pretty easy to do.

A Pre-Requisites

Two things that you need to understand about this solution I’m giving you.

  1. If you are using some kind of plug-in to insert your social media sharing buttons, then this solution won’t work for you. That would require hacking the actual plug-in. So, this solution depends on you having the social media sharing button code included as part of your theme.
  2. If you are using some kind of theme framework, then it might change where and how you insert code modifications.

So, with that said, let’s code…

The Code

First thing you want to do is open up the single.php file of your Wordpress theme. This is the template file for your individual blog posts.

In that file, somewhere before the first social media button appears, you’ll want to insert the following PHP code:

<?php
$sharing_url = get_permalink();
$postid = get_the_ID();
if ($postid < XXXX) {
$sharing_url = str_replace("://www.blogmarketingacademy.com",
"://www.davidrisley.com",
$sharing_url);
}
?>

So, here’s what this code is doing…

  1. It gets the current URL (or permalink) and assigns it to a variable $sharing_url.
  2. It gets the current post ID. This is the ID number for the post inside your database.
  3. It evaluates the post ID to see if it is less than a certain threshold. That XXXX should be replaced with the post ID of the most recent blog post you created JUST BEFORE you made your domain name. The easiest way to get that ID is to hover over the “Edit” button of the post with your cursor inside the WP control panel, look at the URL that the link would go to, and you’ll see the database ID in that URL.
  4. Only if the post ID is earlier than the threshold you’ve defined, it will look for instances of your new domain and rewrite it to the old domain.

At this point, you have a URL which is ready to be shared across social media… and that URL may or may not be the same as your current domain name. If it is an earlier post, it won’t be.

Next, you need to re-code the social sharing buttons so that it uses this $sharing_url rather than defaulting to the current URL.

For Twitter:

<a href="http://twitter.com/share" data-count="vertical" data-via="davidrisley" data-url="<?php echo $sharing_url; ?>">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

Notice the inclusion of the “data-url” field. Consult Twitter retweet button docs if needed.

For Facebook:

<fb:like href="<?php echo $sharing_url; ?>" send="true" layout="box_count" width="60" show_faces="true" font=""></fb:like>

Notice the addition of the href tag. Consult Facebook docs for the actual code, then modify to add the href attribute.

And, for Google Plus: (see +1 button docs if needed)

<g:plusone size="tall" href="<?php echo $sharing_url; ?>"></g:plusone>

I will assume that you know how to manually include the code for the social buttons in your theme file. The above references were only to illustrate how to pass a URL other than default to these networks… and the above code is not the full code necessary to show those buttons.

.

This solution works very nicely. It is a bit of a hack, but it gets the job done.


Got A Question? Need Some Assistance?

Have a question about this article? Need some help with this topic (or anything else)? Send it in and I’ll get back to you personally. If you’re OK with it, I might even use it as the basis of future content so I can make this site most useful.

Question – Lead Form