Last active
October 15, 2015 10:04
-
-
Save itsmikita/11079395 to your computer and use it in GitHub Desktop.
WordPress: Change domain and update all links MySQL procedure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 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; |
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
Use it:
CALL wp_change_domain( 'wp_', 'http://olddomain.com', 'http://newdomain.com' );