All things WordPress

How to rename a WordPress URL in MYSQL

Written by: Peter Fisher on October 12, 2017
Tagged under: , ,
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

WordPressRecently I have been doing a fair amount of WordPress and Docker work.

During last weekends live coding stream I demonstrated how to build a WordPress site using Docker and Composer.

This week I was tasked to rebuild a WordPress site which meant downloading the existing database and building a new WordPress theme.

A common issue that I faced was renaming the WordPress URL which is littered amongst the old database tables.

Each time the databases switched from local to a remote backup the old URL would be stored against the posts and in meta values. It was getting pretty tedious to change these entries manually so a script needed to be built.

Rename WordPress script

This is the solution that I came up with:

This script requires two SQL variables to be set. The first is the old site address and the second is the new one.  I’ve included some examples of how to set and pass these variables into the SQL file.

I needed to do this this from outside the Docker container so I have also included an example which shows how to do this via Docker.

This may prove handy when switching between a local and remote versions of a WordPress website where the domains are different.

Let me know if you found this solution useful. Or if you know a better way to do it then post it in the comments below.

Something that I don’t like about this script is that it exposes the password as you type it.  Normally when you add the -p flag the password is requested but not shown on the terminal but this isn’t the case here. If anyone knows how to solve this then please let me know.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Peter Fisher is a web developer working in Gloucester UK. Founder of the digital agency Websomatic, author of this blog and the HowToCodeWell Youtube channel. Peter has over ten years of web development experience under his belt

Read all about Peter Fisher

Leave a Reply

Your email address will not be published. Required fields are marked *