One of the problems bloggers face when changing domain names is that this removes all recorded social shares.

One of the problems bloggers face when changing domain names is that this removes all recorded social shares. My little blog doesn’t have a ton of social activity, but some of the articles have decent shares and I didn’t want to lose that during a recent domain transfer.

I scoured the web for a good solution but could not find anything automated. For a while, it looked like I would have to manually code social buttons for each article! Yikes! No amount of coffee was going to make that happen.

Programming is not really my expertise but I’m fairly comfortable in a few languages. Fortunately, PHP is one of them.

I’m using a PHP based social plugin that inserts a URL parameter into the button code for each social button. I figured there must be a way to automate changing that URL to the old link to get the social shares to transfer over. At the same time, I didn’t want the URL to change for new articles as they should use the new domain for social sharing.

With my limited skills, I finally managed to tinker out a solution that replaces the domain in the social share URL only if the post is from before the domain switch. I’m not going to share all of the details here so email me if you want those. It was a challenge for me to work out the solution and I didn’t find much help online, so maybe this will help someone else looking to solve this unique problem.

First, I declared a variable called $social_check; and used an IF statement to give different values to that variable based on whether the article number was lower than the last article I posted under the old domain. Make sense? If not, you should probably just jump to a non-programming post!

Second, I retrieved the $social_check; variable in the function that generates the social button code. With another IF statement, I used str_replace() to swap the new domain with the old one in the URL variable of any article from before my domain swap.

I was then able to use the new URL with all social buttons. The end result is that articles from before my updated domain use the old URL for social buttons. Newer articles use the new URL with the new domain. Best of all, all of this happens automatically with no manual coding required!

Now I can get back to enjoying my coffee and writing content instead of code.

Adam Bowers Profile

Adam Bowers

Disciple of Jesus, husband of Jenny, father of 3 awesome kids, Senior Pastor at First Free Church, passionate about growing God's kingdom by developing influencers for Jesus.I still use parts of this site for various ministry purposes even though I haven't written a blog post in a long time!