I have a table called “wp-posts” with a field “post-content”. This field contains the text for a blog posts. I’d like to change all records to replace an URL for another one.
Imagine that I can have things like:
This is a test and somewhere there’s something like <img src=”http://oldurl.com/wp-content/somimg.jpg”> and something like <a href=”http://oldurl.com/something”>a link</a>.”
I want it to be
This is a test and somewhere there’s something like <img src=”http://newurl.com/wp-content/somimg.jpg”> and something like <a href=”http://newurl.com/something”>a link</a>.”
I need to be able to change this for every record in my table without having to open each post in WordPress and change them by hand. There has to be a way to do this
This can be easily achieved with a simple SQL statement using MySQL’s
replace()
function. Before we do that, you should definitely do a database dump or whatever you use for backups. It’s not only that it’s The Right Thing To Doâ¢, but if you make a mistake on your substitution, it might prove difficult to undo it (yes, you could rollback, but you might only figure out your mistake later on.)To create a database dump from MySQL, you can run something like this —
Where (and you probably know this, but for the sake of completeness for future generations…) —
Now that we got that out of the way, you can log in to the MySQL database using:
And simply run this statement:
And that should do it!
If you make a mistake, you can often rerun the statement with the original and new texts inverted (if the new text — in your case the new URL — didn’t already exist in the text before you did the replace.) Sometimes this is not possible depending on what the new text was (again, not likely in your case.) Anyway, you can always try recovering the sql dump —
And voilà .
Use the replace string function in MySQL:
UPDATE MyTable SET textfield = REPLACE(textfield, "http://oldurl.com/", "http://newurl.com")
There’s a handy WordPress plugin that I’ve used to search and replace in posts pages with grep:
http://urbangiraffe.com/plugins/search-regex/
I recently used http://wordpress.org/extend/plugins/search-and-replace/ to update a site from a development server to the public server. I used the plugin to change all URLs of images from their “00.00.00.00/~user/” to their “example.com” format.
It worked perfectly.
Of course, be sure to backup the database first in case you happen to make a typo during the process.
There is a few places to work that out.
Procedure
1. Backup your database
I generally go like so:
I even have this in a crontab on my own server. But that’s an other topic.
2. Try using WordPress internal utility
WordPress has it’s own utility for that matter. Most of the time, it can be done only with it.
Simply add to your theme’s
functions.php
(see wordpress documentation) file the following line:Assuming you are already running the theme that has that particular
functions.php
file.Run the site, by refreshing the page.
Then, comment the lines!
You won’t need them anymore.
Otherwise they get to update your database configuration at every page load.
If it is not enough, continue…
3. Search for matches
I personally use Adminer on my deployment, it is a lightweight one-file replacement for PHPMyAdmin.
Why I suggest Adminer? It has a nice search all database feature that I find quite handy in our use-case.
It is a search box, labelled: “Search in data tables”.
4. Write your own search and replace
Note that in my case, I was migrating ALL my urls from NON HTTPS to HTTPS.
Hope that was helpful