Skip to content

Instantly share code, notes, and snippets.

@Zszywaczyk
Last active October 29, 2023 04:09
Show Gist options
  • Save Zszywaczyk/8e704128875040f3154205f7589e1316 to your computer and use it in GitHub Desktop.
Save Zszywaczyk/8e704128875040f3154205f7589e1316 to your computer and use it in GitHub Desktop.
Wordpress MySQL script to change URL from old to new
//Not tested yet!
1. Create bash script eg. bash.sh
2. Write:
#!/bin/bash
DB_USER="your_db_user"
DB_PASS="your_db_password"
DB_NAME="your_db_name"
mysql -u$DB_USER -p$DB_PASS $DB_NAME << EOF
$(mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "
SELECT CONCAT('UPDATE ', table_name, ' SET ', column_name, ' = REPLACE(', column_name, ', \'old_url\', \'new_url\');') AS query
FROM information_schema.columns
WHERE table_schema = '$DB_NAME'
AND data_type IN ('char', 'varchar', 'text');
")
EOF
3. run script by typing ./bash.sh
SET @old_url = 'old.example.com';
SET @new_url = 'new.example.com';
UPDATE wp_comments SET comment_author_url = replace(comment_author_url, @old_url, @new_url);
UPDATE wp_comments SET comment_content = replace(comment_content, @old_url, @new_url);
UPDATE wp_newsletter SET http_referer = replace(http_referer, @old_url, @new_url);
UPDATE wp_options SET option_value = replace(option_value, @old_url, @new_url);
UPDATE wp_postmeta SET meta_value = replace(meta_value, @old_url, @new_url);
UPDATE wp_posts SET post_content = replace(post_content, @old_url, @new_url);
UPDATE wp_posts SET guid = replace(guid, @old_url, @new_url);
UPDATE wp_super SET ivalues = replace(ivalues, @old_url, @new_url);
UPDATE wp_term_taxonomy SET description = replace(description, @old_url, @new_url);
UPDATE wp_yoast_indexable SET permalink = replace(permalink, @old_url, @new_url);
UPDATE wp_yoast_indexable SET title = replace(title, @old_url, @new_url);
UPDATE wp_yoast_indexable SET twitter_image = replace(description, @old_url, @new_url);
UPDATE wp_yoast_indexable SET open_graph_image = replace(open_graph_image, @old_url, @new_url);
UPDATE wp_yoast_indexable SET open_graph_image_meta = replace(open_graph_image_meta, @old_url, @new_url);
UPDATE wp_yoast_seo_links SET url = replace(url, @old_url, @new_url);
/*Change, remove or add more tables*/
wp search-replace https://old.example.com http://new.example.com --all-tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment