覆盖WordPress多站点的WP_SITEURL和WP_HOME [英] Override WP_SITEURL and WP_HOME for WordPress Multisite

查看:1061
本文介绍了覆盖WordPress多站点的WP_SITEURL和WP_HOME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在WordPress网站( http://www.example.com )上进行本地开发,我是之前覆盖 wp-config.php WP_SITEURL WP_HOME c> like like:

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.

然后需要将安装转换为WordPress多站点,以便用户,身份验证,插件等可以共享主网站和辅助网站( http://second.example.com )。

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).

上面覆盖 wp_options 表中的值的方法不再有效,但我不确定设置主表和子域的 wp_blogs 中的条目以及 wp_2_options 表中的条目。

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.

更新我的 HOSTS 文件是一种解决方法,但它不是理想的(我不能比较现场网站等) 。运行脚本来更改数据库值是我尝试的另一个选项,但是稍微更麻烦,所以我的问题是,是否有MultiSite中的一个选项来覆盖设置文件中的这些值,例如 wp-config.php ,如果是这样,它将是什么样子。

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.

推荐答案


更新:完整更新的插件代码及其附加说明可在这里找到: http://justinsilver.com/technology/wordpress/wordpress-plugins/wordpress-plugin-wp-server-migration/


我能够在@ user916011的帮助下找到一个解决方案。我需要能够将 wp_options 表复制到我的开发环境,因为它们包含所需的配置。为了克服无法在MultiSite中设置WP_SITEURL和WP_HOME值的问题,我编写了一个自定义过滤器来替换 _config_wp_siteurl() _config_wp_home ()可用于非多站点安装的函数,它包含在网络范围内并在 wp-config.php 。然后,我可以将除了 wp_site wp_blogs 的所有数据库表本地数据库。


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.

我强烈建议您使用 WordPress 3.0的URL令牌替换技术文章Chris Murphy帮助处理你的

I highly recommend the URL Token Replacement Techniques for WordPress 3.0 article by Chris Murphy to help handle URLs in your content.

此示例假设一个子域多站点安装,域名为 example.com 和两个子域名 www.example.com second.example.com 。本地开发URL分别为 www.example.local second.example.local

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.

数据库更改

更新 wp_site

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

更新 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';

插件代码
以下插件应安装网络-wide。

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'    );
?>

配置wp-config.php

wp-config.php 添加新常数。主站点应使用标准 WP_HOME WP_SITEURL ,第三个URL应使用 WP_ { $ blog_id} _HOME WP _ {$ blog_id} _SITEURL

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');

这篇关于覆盖WordPress多站点的WP_SITEURL和WP_HOME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆