Web Development by Solarise

The Solarise.dev Blog

WordPress search & replace text functionality with Search Replace DB

Blog archive Robin Metcalfe 9th December 2017

We’ve all done it. The development database is ready for production, the client has populated all the fields and everything is ready to move to the live server. All runs smoothly but then, what’s this? Why are those images not loading? Why, that’s because they’re referencing your local development server. Silly billy.

Oh butterfingers

These days, I have a checklist of items to run through prior to setting a site live, but it wasn’t always such plain sailing.

One of the biggest hiccups I’ve encountered in the past is in ensuring that the content populated on a development site is transferred to live complete with all necessary changes made to the data so that links still work and images are properly referenced.

Using WordPress, within theme and template files, you can make use of helper functions such as bloginfo() to ensure that theme code always references the current site. No links to urls are ever hardcoded within the template.

echo get_bloginfo('url'); // prints out the site URL

However, the populated content is another matter. Clients or developers may well drop absolute links to images or pages into the backend WYSIWYG. These will pretty much always be referencing the local development URL, leaving strings such as these lying around.

http://dev.stagingenvironment.client.com/site_a/img/hello.jpg

But, no fear. Help is at hand. This handy little PHP script will let you swap out text strings and replace with new.

Multicolumn swap shop

You can use this for various purposes, such as

  • Replacing all instances of http:// with https:// within content
  • Swapping out names or other references without hassle, for example if a client wants a global change to a product name site-wide
  • Swapping out all development URLs with live

The latter there is the one I find myself doing most. Installation is straightforward, just follow the instructions on the site and within a few minutes you’ll be good to go.

The tool cleverly picks up on your WordPress’ installation’s database credentials (as long as you place it within the site’s root directory) and also gives you the option to do a dry run through your database before making any changes (recommended!)

You’ll then be prompted to enter the text you wish to replace, and what you want to replace it with. In our example above, we might provide the following as our source string:

dev.stagingenvironment.client.com/site_a

And this as our destination string, to replace the source string across all instances throughout the database

www.client.com

Be aware when making these adjustments that you have considered how the replacement will function. If you, for example, try replacing http://dev.stagingenvironment.client.com then this may not pick up any instances of https. You may have to run two or three replacements to catch all variations of a string.

Take note as well, that the script will offer to leave WordPress’ GUID fields untouched. This is important for live/production servers which have had content indexed. The GUID is important for RSS purposes, and shouldn’t be changed without a good deal of thinking first.

A couple of other important points – these hopefully don’t need saying, but just incase,

  • Make a backup of your data first. Especially if it’s important client data. (You should be doing this anyway!)
  • Do not leave a copy of this script available on your dev/live server. It goes without saying that this tool is massively powerful, giving anyone with access the ability to change/insert any data in your database.

Search Replace DB

Author Image

About the author

Robin is the dedicated developer behind Solarise.dev. With years of experience in web development, he's committed to delivering high-quality solutions and ensuring websites run smoothly. Always eager to learn and adapt to the ever-changing digital landscape, Robin believes in the power of collaboration and sharing knowledge. Outside of coding, he enjoys diving into tech news and exploring new tools in the industry.

If you'd like to get in touch to discuss a project, or if you'd just like to ask a question, fill in the form below.

Get in touch

Send me a message and I'll get back to you as soon as possible. Ask me about anything - web development, project proposals or just say hi!

Please enable JavaScript in your browser to complete this form.

Solarise.dev Services

Maintenance Retainers

Min 3 hours per month, no contract

Project Planning

Get in touch to discuss your project details

Consultancy

Face-to-face chats. Solve your problems!