Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override WP_SITEURL and WP_HOME for WordPress Multisite

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.

like image 834
doublesharp Avatar asked Oct 18 '12 15:10

doublesharp


2 Answers


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');
like image 77
doublesharp Avatar answered Oct 31 '22 20:10

doublesharp


You could use the update_option in functions.php

update_option("siteurl","http://example.com");
update_option("home","http://example.com");
like image 1
Samuel Cook Avatar answered Oct 31 '22 19:10

Samuel Cook