Skip to Content

wordpress-update-siteurl

The issue

When we copy a wordpress site, we have to update the ‘home’ and ‘siteurl’ to make the site working,

But it is annoying to update everytime with wp-cli:

    wp option update home 'https://example.com'
    wp option update siteurl 'https://example.com'

The solution

The simple way would be to add the following line into wp-config.php: (assume your site are in HTTPS)

    define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
    define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);

This configuration will get virtualhost name dynamicly for the site and overwrite the DB config, so no more manual updates anymore.

Update contents

Sometimes we also need to update hard written links from posts, here is steps:

  1. Make a full backup of your DB.

  2. Use wpcli dryrun to see what will change:

    wp search-replace 'qa.example.com' 'www.example.com' --recurse-objects --skip-columns=guid --dry-run
    
  3. Remove ‘–dry-run’ and make the update:

    wp search-replace 'qa.example.com' 'www.example.com' --recurse-objects --skip-columns=guid
    
  4. Remember to clear cache if you have cache plugins like super-cache enabled.

Read more on WP-CLI search-replace help