How To Keep Your Social Media Buttons From Showing Zero After A Domain Change [WordPress]

Last Updated on June 3, 2012  

David Risley
Founder, Lead Instructor of Blog Marketing Academy

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:

Afterward, the URL was:

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:

$sharing_url = get_permalink();
$postid = get_the_ID();
if ($postid < XXXX) {
$sharing_url = str_replace("://",

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="" data-count="vertical" data-via="davidrisley" data-url="<?php echo $sharing_url; ?>">Tweet</a><script type="text/javascript" src=""></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.


Related Articles You Might Like:

  • Alison Moore Smith says:

    Thanks for this. I have a client who wants to rebrand and I was researching how to accomplish this.

    Question: If you have modded the sharing buttons for an old post, it looks like it cannot continue to accumulate social shares. In other words, the count will be “stuck” at the point it was when the switch occurred. Is that correct?

  • No Let Squash says:

    I suppose this should work for blogger too.

  • I’ve been scouting the net for hours but I couldn’t find the “Blogger” counterpart of this solution. Whew.  And I’m not really a techie guy so this is gonna be a biggg problem for me.

    Thanks for the post though, I’ll just continue looking. 

  • business logo design says:

    Very important points you have shared and there is no doubt that it is very important point and a burning issue after a domain change. I learn a lot of thing from your very keen and comprehensive post.

  • Dolphin247seo says:

    Your article is very useful and interesting, keep writing boss.

  • ianbrodie says:

    Very neat. I’m thinking of collapsing the URL structure of my site to simplify and take out the category names I have in there. I could tweak this code to make that work without losing my social share counts.



  • Chris Huff says:

    Thanks for posting up this stuff as you work through it. 
    I opted for a domain name associated with my niche.  However, branding a name is totally understandable as well.  I”m not sure I’ve seen many make that switch.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}