0

I want to migrate my development wordpress site to the production domain. In order to success the migration I found and replace all previous url occurences (http://localhost) to the new (https://mywebsite.com). The problem is that old urls cannot be replaced directly in the field option_value of theme_mods_{your theme name} in the table wp_options because data in there are serialized.

How can I replace old urls by the new without breaking theme settings ?

  • You shouldn't store URLs, store attachment post IDs instead then use the API functions to get the URL back. Then you won't need to change them when you migrate the site. Have you looked at the WP CLI search replace command? – Tom J Nowell Jul 30 at 15:38
1

Use the official CLI tool:

wp search-replace 'http://localhost' 'https://yoursitecom'

It will automatically deserialize any post meta, options, theme mods, etc and adjust them to match the new URL.

Fundamentally though, it's bad practice to store URLs to images posts and assets in the database. Store the post ID instead, and this problem goes away

  • I agree with you for the bad practice ouf using url but the my theme’s cutomizer (hestia) uses absolute urls. – Louis D. Jul 30 at 23:08
0

It's possible to search and replace old urls to new ones by using maybe_unserialize(get_theme_mods()) and get_theme_mod() WP functions and following these steps:

  1. Create a php file in the root folder of your WP installation (same path as wp-load.php).
  2. Insert the following code :
require_once("wp-load.php");
$r = maybe_unserialize(get_theme_mods());
foreach ($r as $k => $v){
    if(!empty($k)){
        $ListOptions[] = $k;
    }
}
foreach($ListOptions as $option){
    $str = get_theme_mod($option);
    $str = str_replace('http://localhost', 'https://mywebsite.com', $str);
    $str = str_replace('http:\\/\\/localhost', 'https:\\/\\/mediadroit.fr', $str);
    set_theme_mod($option, $str);
    var_dump('|'.$option.': '.get_theme_mod($option).' ===> '.$str);
    echo "\n";
}
  1. Execute the script
  • I indented your code, but this would be better off as a WP CLI command – Tom J Nowell Jul 30 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.