For doing local development on a WordPress site (http://www.example.com), I was previously overriding the WP_SITEURL
and WP_HOME
values in wp-config.php
like so:
define('WP_SITEURL', 'http://local-example/');
define('WP_HOME', 'http://local-example/');
This would allow me to copy the database and site files to a local server, and make modifications as necessary, testing on the local install.
It was then necessary to convert the install to a WordPress Multisite so that users, authentication, plugins, etc. could be shared between the main site and a secondary site, hosted on a subdomain (http://second.example.com).
The method above to override the values in the wp_options
table no longer works, but I am unsure the proper way to set a value for the entries in wp_blogs
as well as the wp_2_options
table for the primary and subdomain.
Updating my HOSTS
file is somewhat of a workaround, but it not ideal (I am not able to compare to the live site, etc). Running a script to change the database values is another option I have tried, but is slightly more cumbersome, so my questions is whether or not there is an option in MultiSite to override these values in a settings file, such as wp-config.php
, and if so what it would look like.
Update: full updated plugin code with additional description can be found here: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-wp-server-migration/
I was able to come up with a solution with the help of @user916011. I needed to be able to copy the
wp_options
table(s) to my development environment as they contain configurations that are needed. To overcome the issue of not being able to set the WP_SITEURL and WP_HOME values in MultiSite, I wrote a custom filter to replace the
_config_wp_siteurl()
and
_config_wp_home()
functions that are available for non-multisite installs that is included in a plugin that is available network-wide and is configured in
wp-config.php
. I am then able to copy all of the database tables
except wp_site
and
wp_blogs
to a local database.
I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.
This example assumes a subdomain multisite install, with a domain of example.com
and two subdomains, www.example.com
and second.example.com
. The local development URLs will be www.example.local
and second.example.local
respectively.
Database Changes:
Update the domain value in wp_site
:
UPDATE wp_site SET domain = 'example.local' WHERE domain = 'example.com';
Update the domain value(s) in wp_blogs
:
UPDATE wp_blogs SET domain = 'www.example.local' WHERE domain = 'www.example.com';
UPDATE wp_blogs SET domain = 'second.example.local' WHERE domain = 'second.example.com';
Plugin Code:
The following plugin should be installed network-wide.
<?php
/*
Plugin Name: MultiSite WP_HOME and WP_SITEURL
Plugin URI: http://doublesharp.com/
Description: Allows wp_options values to be overwritten in wp-config.php for MultiSite
Author: Justin Silver
Version: 1.0
Author URI: http://doublesharp.com
License: GPL2
*/
function _ms_config_wp_siteurl( $url = '' ) {
if (is_multisite()):
global $blog_id, $current_site;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'SITEURL';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_siteurl', '_ms_config_wp_siteurl' );
function _ms_config_wp_home( $url = '' ) {
if (is_multisite()):
global $blog_id;
$cur_blog_id = defined('BLOG_ID_CURRENT_SITE')? BLOG_ID_CURRENT_SITE : 1;
$key = ($blog_id!=$cur_blog_id)? $blog_id.'_' : '';
$constant = 'WP_'.$key.'HOME';
if ( defined( $constant ) )
return untrailingslashit( constant($constant) );
endif;
return $url;
}
add_filter( 'option_home', '_ms_config_wp_home' );
?>
Configure wp-config.php:
Add new constants to wp-config.php
. The primary site should use the standard WP_HOME
and WP_SITEURL
and the tertiary URLs should use WP_{$blog_id}_HOME
and WP_{$blog_id}_SITEURL
define('WP_HOME', 'http://www.example.local');
define('WP_SITEURL', 'http://www.example.local');
define('WP_2_HOME', 'http://secondary.example.local');
define('WP_2_SITEURL', 'http://secondary.example.local');
You could use the update_option in functions.php
update_option("siteurl","http://example.com");
update_option("home","http://example.com");
There's a similar question being asked here: Team Development of a Wordpress Site which I provided a possible solution for. In your case, you may not want to go to that extent (though it would be very flexible); however, you could always look at the portion of the answer that mentions a domain-replacement technique.
I've outlined that solution here: URL Token Replacement Techniques...
I had the same issue and wanted a solution as similar to defining WP_HOME & WP_SITEURL in wp-config.php as possible.
I can't use a plugin, because I am syncing with GIT and don't want to have that plugin in my repo and I would have to add the plugin everytime... I suppose it could be activated network wide through the wp_sitemeta table... but it wasn't ideal for me.
I came up with this solution.
Be sure to not sync wp_blogs, wp_site, wp_sitemeta. And then add this code to your local wp-config.php somewhere below $table_prefix
:
/* Update local database */
mysql_connect( DB_HOST, DB_USER, DB_PASSWORD ) or die( mysql_error() );
mysql_select_db( DB_NAME );
$result = mysql_query("SELECT * FROM `". $table_prefix ."blogs`") or die( mysql_error() );
while( $row = mysql_fetch_array( $result ) )
{
$id = (1 == $row['blog_id']) ? '' : $row['blog_id'] .'_';
mysql_query( "UPDATE `". $table_prefix . $id ."options` SET `option_value`='http://". $row['domain'] ."/' WHERE (`option_name`='siteurl' OR `option_name`='home')" ) or die( mysql_error() );
}
This will make sure your sites are synced up to your local wp_blogs table.
The only drawback is that when you add a new site, you do manually need to copy it into your wp_blogs table and update its local url.