mySQL Find and Replace throughout Entire WordPress Database
Just finished writing this very handy Script for updating all links in a wordpress site should you choose to move your domain.
This goes through every column of every table in the database and replaces string searched with the desired string.
This code should be placed in “functions.php” in the base directory of the theme you are currently using in WordPress.
You should place this code in the functions file and then load your site only ONCE. Remove the code once the process is complete. You will see the results of the query echo out line by line.
This code will also work on any site using the ezSQL wrapper. *Note: If you have serialized arrays that may be effected, this will not work, as the length of the string may be different than what was originally set…
- $global $wpdb;
- $find_this = “/home/”;
- $replace_with_this = “/”;
- $database = $wpdb->get_results(“SHOW TABLES”);
- foreach($database as $database_tables):
- foreach($database_tables as $database_table):
- $table = $wpdb->get_results(“SHOW COLUMNS FROM $database_table”);
- foreach($table as $column):
- $wpdb->query(“UPDATE $database_table SET $column->Field = REPLACE($column->Field,’$find_this’,’$replace_with_this’)”);
- echo “UPDATE $database_table SET $column->Field = REPLACE($column->Field,’$find_this’,’$replace_with_this’)”;