I’m working on a WordPress MU blog network and ran accross the need to change the domain that the blogs are running under. This should actually be a fairly common need, for example, if you copy your PROD environment to QA, you’ll probably want to change the blog url from http://blog.com to http://qa.blog.com.

Changing the domain name starts out pretty straightforward:

  • edit the “domain” in the wp_site table.
  • edit all “domain” entries in the wp_blogs table: UPDATE wp_blogs SET domain = REPLACE(domain,”blog.com”,”qa.blog.com”)

But then, you’ll notice that there’s a lot of hard-coded urls in the wp_#_options table, which exists for every blog in your network.

So, here’s a little PHP file that you can put in the root of your WordPress MU install (or elsewhere). This will loop though these tables and do the replaces you need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
< ?php
require_once('wp-load.php');
 
$querystr ="SHOW TABLES LIKE 'wp_%_options'";
$tables = $wpdb->get_results($querystr, ARRAY_N);
echo count($tables);
$query = "";
if ($tables){
	foreach ($tables as $table){
		$query = 'UPDATE '.$table[0].' SET option_value = REPLACE(option_value,"blog.com","qa.blog.com")';
		$wpdb->query($query);
	}
}
?>

The file will output a count of the number of tables that were affected. Ta da