0

I recently moved my WordPress network to a new domain and thought I had updated every reference to that domin, however I was wrong. I searched high and low for the answey, but alas I could find it no where, so I thought I'd write this question/answer.

In my scanario, the 'home' site on the network is a simple page that lists all of the other sites that are currently active, but the network_site_url({blog path}) was returning a link to the site on the old domain.

network_site_url() uses get_current_site() to return an object relating to the current site, and it was that object that listed the domain incorrectly.

function get_current_site() {
    global $current_site;
    return $current_site;
}

Dispite my efforts, I could not find where the $current_site global was declared.

2

The database

Initially you should update all references to your old domain in the database. The following SQL will take care of that, but first read these notes -

  • This SQL assumes that you are using the default wp_ table prefix. Simply change this to your chosen table prefix if you have changed it.
  • Replace database_name with the name of the database which is used by WordPress.
  • Replace www.old-domain.com with the name of the domain from which you are moving away from.
  • Replace www.new-domain.com with the name of the domain that you are moving to.
  • While it's unlikely, there may be other references to the old domain (in wp_postmeta for example). To check, in PHPMyAdmin you should select the databse, click Search and search for any references to %www.old-domain.com% in all tables, and then manually update them as required.

Important - The example below will only update references to the old domain for the main site and the first child site. Should you have more sites you will need to replicate the code for the UPDATE wp_*_options... and UPDATE wp_*_posts queries, where * is the ID of the site.

USE `database_name`;
UPDATE `wp_options` AS `o1` SET `o1`.`option_value` = REPLACE(`o1`.`option_value`, 'www.old-domain.com', 'www.new-domain.com') WHERE `o1`.`option_name` IN (
    'siteurl',
    'home'
);
UPDATE `wp_options` AS `o2` SET `o2`.`option_value` = REPLACE(`o2`.`option_value`, 'www.old-domain.com', 'www.new-domain.com') WHERE `o2`.`option_name` IN (
    'siteurl',
    'home'
);
UPDATE `wp_posts` AS `p1` SET `p1`.`guid` = REPLACE(`p1`.`guid`, 'www.old-domain.com', 'www.new-domain.com');
UPDATE `wp_2_posts` AS `p2` SET `p2`.`guid` = REPLACE(`p2`.`guid`, 'www.old-domain.com', 'www.new-domain.com');
UPDATE `wp_blogs` AS `b` SET `b`.`domain` = REPLACE(`b`.`domain`, 'www.old-domain.com', 'www.new-domain.com');
UPDATE `wp_site` AS `s` SET `s`.`domain` = REPLACE(`s`.`domain`, 'www.old-domain.com', 'www.new-domain.com');
UPDATE `wp_sitemeta` AS `sm` SET `sm`.`meta_value` = REPLACE(`sm`.`meta_value`, 'www.old-domain.com', 'www.new-domain.com');
UPDATE `wp_usermeta` AS `um` SET `um`.`meta_value` = REPLACE(`um`.`meta_value`, 'www.old-domain.com', 'www.new-domain.com');

The important bit that I missed

In a network installation a DOMAIN_CURRENT_SITE constant is declared in wp-config.php. Simply open that file and update the value of that constant to your new domain and you should be good to go.


One more consideration

In some cases WP_HOME and WP_SITEURL constants may be declared in wp-config.php, and these will of course override any entries in the database. I'd suggest searching the file for those entries just in case, and if they are present I'd recommed removing them (or at least commenting them out), as it's much better to use the DB entries.

define('WP_HOME', 'http://www.old-domain.com');
define('WP_SITEURL', 'http://www.old-domain.com');
0

Also you can use: Search Replace DB

And do:

replace: "olddomaim.com" with: "newdomain.com"
and > "live run"

If you need replace nowwwdomain.com to www.domain.com at first do:

replace: "www.domain.com" with: "domain.com"
and > "live run"

Then:

replace: "domain.com" with: "www.domain.com"
and > "live run"

Because else you can get issues with "www.www.domain.com"

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.