Programster's Blog

Tutorials focusing on Linux, programming, and open-source

WordPress - Allow Easy Change Of Domain

Steps

Add the following to your wp-config.php before changing your domain.

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

Now when you go to your site from a different domain, it doesn't matter.

A Note About Trying To Change Domain Through Database Queries

The theme customizations are stored in a PHP serialized array. This serialization method counts the number of characters in the string values. E.g.

;s:200:"http://www.mydomain.com/wp-content/uploads/2019/07/logo-alt-2.png";

After you replace the values for your old domain to your new one using something like:

UPDATE `wp_options` SET option_value = REPLACE(option_value, 'old.domain.com', 'my.new.domain.com');

... then the string count/length will not match, and wordpress will consider the value corrupted, and reset your theme customizations.

References

Last updated: 2nd July 2021
First published: 5th August 2019