Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Last active October 15, 2015 10:04
Show Gist options
  • Save itsmikita/11079395 to your computer and use it in GitHub Desktop.
Save itsmikita/11079395 to your computer and use it in GitHub Desktop.
WordPress: Change domain and update all links MySQL procedure
--
-- Change domain and update all links
--
-- @param prefix - Table prefix
-- @param old - Old URL including http://, used to search for links in posts, etc.
-- @param new - The new URL including http://
--
SET @prefix = 'wp_';
SET @old = 'http://';
SET @new = 'http://';
SET @update_options_sql = CONCAT( "UPDATE ", @prefix, "options SET option_value = REPLACE( option_value, '", @old, "', '", @new, "' ) WHERE option_name IN ( 'home', 'siteurl' )" );
SET @update_posts_sql = CONCAT( "UPDATE ", @prefix, "posts SET post_content = REPLACE( post_content, '", @old, "', '", @new, "' ), guid = REPLACE( guid, '", @old, "', '", @new, "' )" );
SET @update_postmeta_sql = CONCAT( "UPDATE ", @prefix, "postmeta SET meta_value = REPLACE( meta_value, '", @old, "', '", @new, "' )" );
SET @update_links_sql = CONCAT( "UPDATE ", @prefix, "links SET link_url = REPLACE( link_url, '", @old, "', '", @new, "' )" );
PREPARE stmt FROM @update_options_sql; EXECUTE stmt;
PREPARE stmt FROM @update_posts_sql; EXECUTE stmt;
PREPARE stmt FROM @update_postmeta_sql; EXECUTE stmt;
PREPARE stmt FROM @update_links_sql; EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@itsmikita
Copy link
Author

Use it:
CALL wp_change_domain( 'wp_', 'http://olddomain.com', 'http://newdomain.com' );

@itsmikita
Copy link
Author

If you're moving a multisite setup, don't forget to check wp_sites and wp_blogs tables as well as DOMAIN_CURRENT_SITE constant in wp-config.php.

Enjoy your coffee! ;)

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