Recently 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.