Skip to content

Instantly share code, notes, and snippets.

@wesleytodd
Created January 27, 2013 23:00
Show Gist options
  • Save wesleytodd/4651124 to your computer and use it in GitHub Desktop.
Save wesleytodd/4651124 to your computer and use it in GitHub Desktop.
Change the url of a WordPress site.
SET @old_url = "http://oldside.com";
SET @new_url = "http://newsite.com";
SET @prefix = "wp_";
SET @s1 = CONCAT("UPDATE ", @prefix, "options SET option_value = replace(option_value, '", @old_url, "', '", @new_url, "') WHERE option_name = 'home' OR option_name = 'siteurl';");
PREPARE s1 FROM @s1;
EXECUTE s1;
SET @s2 = CONCAT("UPDATE ", @prefix, "posts SET guid = replace(guid, '", @old_url, "', '", @new_url, "');");
PREPARE s2 FROM @s2;
EXECUTE s2;
SET @s3 = CONCAT("UPDATE ", @prefix, "posts SET post_content = replace(post_content, '", @old_url, "', '", @new_url, "');");
PREPARE s3 FROM @s3;
EXECUTE s3;
SET @s4 = CONCAT("UPDATE ", @prefix, "postmeta SET meta_value = replace(meta_value, '", @old_url, "', '", @new_url, "');");
PREPARE s4 FROM @s4;
EXECUTE s4;
@mastef
Copy link

mastef commented Aug 6, 2013

The problem with the method are serialized options, which have the string length defined in them. So if old_url and new_url differ in length, you might destroy your serialized data.

For this I'm using http://interconnectit.com/products/search-and-replace-for-wordpress-databases/ instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment