Override WP_SITEURL and WP_HOME for WordPress Multisite

For local development on the WordPress site ( http://www.example.com ), I previously redefined the values ​​of WP_SITEURL and WP_HOME in wp-config.php as follows:

 define('WP_SITEURL', 'http://local-example/'); define('WP_HOME', 'http://local-example/'); 

This will allow me to copy the database and site files to the local server and make the necessary changes, testing the local installation.

Then it was necessary to convert the installation to WordPress Multisite so that users, authentication, plugins, etc. could be shared between the primary site and the secondary site hosted on a subdomain ( http://second.example.com ).

The method described above for overriding values ​​in the wp_options table no longer works, but I don’t know how to correctly set the value for entries in wp_blogs , as well as the wp_2_options table for the primary and subdomains.

Updating my HOSTS file is a workaround, but it is not perfect (I cannot compare it with a live site, etc.). Running a script to change database values ​​is another option I tried, but a little more cumbersome, so my questions are whether there is an option in MultiSite to override these values ​​in the settings file, for example wp-config.php , and if so , then how it will look.

+7
source share
4 answers


Update: The 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 using @ user916011. I needed to copy the wp_options tables into my development environment since they contain the necessary configurations. To solve the problem of the inability to set the values ​​of WP_SITEURL and WP_HOME in MultiSite, I wrote a special filter to replace the _config_wp_siteurl() and _config_wp_home() functions that are available for non-multisite installations, which are included in the plugin, which is available on a network-wide basis and configured in wp-config.php . Then I can copy all the database tables except wp_site and wp_blogs to the local database.

I highly recommend the WordPress 3.0 URL Token Replacement Methods written by Chris Murphy to handle the URLs in your content.

This example assumes that the subdomain uses a multi-node installation with the example.com domain 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 domain value in wp_site :

 UPDATE wp_site SET domain = 'example.local' WHERE domain = 'example.com'; 

Update domain values ​​(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 must be installed throughout the network.

 <?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 main site should use standard WP_HOME and WP_SITEURL , and 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'); 
+9
source

You can use update_option in functions.php

 update_option("siteurl","http://example.com"); update_option("home","http://example.com"); 
+1
source

A similar question is asked here: Development of the Wordpress site team , on which I presented a possible solution. In your case, you may not want to go to that extent (although it will be very flexible); however, you can always look at the part of the answer that mentions the domain replacement method.

I outlined this solution here: Methods for replacing a URL token ...

+1
source

I had the same problem and wanted the solution to be as similar to defining WP_HOME and WP_SITEURL in wp-config.php as possible.

I can’t use the plugin because I synchronize with GIT and don’t want to have this plugin in my repo and I have to add the plugin every time ... I suppose it can be activated via the wp_sitemeta table network ... but it wasn’t perfect for me.

I came up with this solution.

Be sure not to sync wp_blogs, wp_site, wp_sitemeta. And then add this code to the 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 ensure that your sites are in sync with your local wp_blogs table.

The only drawback is that when adding a new site you need to manually copy it to the wp_blogs table and update the local url.

+1
source

All Articles