Wordpress: How to fix the “Site address” during publication


How many times happens that, after developing a WordPress website on you local environment, after publishing the new shining website to live, something is broken!?!

What is that? Well, after building all the structure, articles etc on localhost (or anywhere else) nothing works?

Yes, this is due to a Site Address reference stored into the database. Actually, there are really many references to http://localhost/ and changing everything by hand is just a nightmare. Just think about all the internal links between posts / articles, images and so on.

Here the following script comes to help. To execute it you can use one of the thousand tools available like PHPMyAdmin, SequelPro, MySQLWorkbench etc..

You just have to

NB: Be careful to the trailing slashes: if you add a trailing slash to A, it should be done to B as well!

-- search
SET @A = "http://A";
-- replace
SET @B = "http://B";

-- wp_comments
UPDATE wp_comments SET comment_content = REPLACE(comment_content, @A, @B);
-- wp_options
UPDATE wp_options SET option_value = REPLACE(option_value, @A, @B);
-- wp_postmeta
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, @A, @B);
-- wp_posts
UPDATE wp_posts SET post_content = REPLACE(post_content, @A, @B);
UPDATE wp_posts SET guid = REPLACE(guid, @A, @B);