Created
December 2, 2015 19:12
-
-
Save mohsinrasool/972254811ce8d9c53023 to your computer and use it in GitHub Desktop.
Following scripts adds two SQL Procedures to move a WP network from main domain to sub directory ( here "/blogs" ).
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
/* | |
Following scripts adds two SQL Procedures to move a WP network from main domain to sub directory ( here "/blogs" ). | |
run_command: to execute a sql query | |
update_options_tables: loop through all the options table and update the home and siteurl options | |
script at the end: updates the wp_blogs and wp_site table plus class the update_options_tables procedure | |
*/ | |
DROP PROCEDURE `run_command`; | |
CREATE DEFINER=`%`@`%` PROCEDURE `run_command` | |
(IN `cmd` VARCHAR(255)) | |
NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN | |
set @command = cmd; | |
PREPARE stmt FROM @command; | |
EXECUTE stmt; | |
DEALLOCATE PREPARE stmt; | |
END | |
DROP PROCEDURE `update_options_tables`; | |
CREATE DEFINER=`%`@`%` PROCEDURE `edit_sub_tables` | |
(IN `old_url` VARCHAR(255), IN `new_url` VARCHAR(255)) | |
NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN | |
DECLARE done INT DEFAULT FALSE; | |
DECLARE current_table CHAR(32); | |
DECLARE sub_site_tables CURSOR FOR SELECT distinct table_name FROM information_schema.tables WHERE table_name like 'wp_%_options'; | |
-- you can use "in ('wp_1_options','wp_2_options','wp_3_options','wp_4_options');" above as well | |
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; | |
OPEN sub_site_tables; | |
read_loop: LOOP | |
FETCH sub_site_tables INTO current_table; | |
IF done THEN | |
LEAVE read_loop; | |
END IF; | |
SET @sql_text = concat('update ', current_table,' set option_value = replace(option_value, "',old_url,'","', new_url,'") where option_name IN( "siteurl","home") '); | |
CALL run_command(@sql_text); | |
END LOOP; | |
CLOSE sub_site_tables; | |
END | |
UPDATE `wp_blogs` SET `domain` = 'new_site.com', `path`= concat('/blogs',path); | |
UPDATE `wp_site` SET `domain` = 'new_site.com', `path`= concat('/blogs',path); | |
call update_options_tables('old_site.com', 'new_site.com/blogs'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment