PHP Wordpress:在不同域之间共享数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress:在不同域之间共享数据库相关的知识,希望对你有一定的参考价值。

Install the second WP as normal into the same database as the original with a DIFFERENT table prefix. Activate the theme you want to use.

Now edit these files... 

In wp-config: 

define('CUSTOM_CAPABILITIES_PREFIX', 'wp_');
define('CUSTOM_USER_TABLE', 'wp_users');
define('CUSTOM_USER_META_TABLE', 'wp_usermeta');

In wp-includes/capabilities.php:
/**
	 * Set up capability object properties.
	 *
	 * Will set the value for the 'cap_key' property to current database table
	 * prefix, followed by 'capabilities'. Will then check to see if the
	 * property matching the 'cap_key' exists and is an array. If so, it will be
	 * used.
	 *
	 * @since 2.1.0
	 *
	 * @param string $cap_key Optional capability key
	 * @access protected
	 */
	function _init_caps( $cap_key = '' ) {
		global $wpdb;
		if ( empty($cap_key) )
			if (defined ('CUSTOM_CAPABILITIES_PREFIX')) {
    	  $this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities';
    	} else {	
        $this->cap_key = $wpdb->prefix . 'capabilities';
    	}
		else
			$this->cap_key = $cap_key;
		$this->caps = &$this->{$this->cap_key};
		if ( ! is_array( $this->caps ) )
			$this->caps = array();
		$this->get_role_caps();
	}

In the active theme's functions.php:
function table_prefix_switch() {
    global $wpdb;
    $options = $wpdb->options; //Save the site 2 options table
    $wpdb->set_prefix('wp_'); //The prefix to site 1
    $wpdb->options = $options; //Put the options table back
}
add_action('init', 'table_prefix_switch');

以上是关于PHP Wordpress:在不同域之间共享数据库的主要内容,如果未能解决你的问题,请参考以下文章

在同一域上的站点之间共享 cookie - Headless / Decoupled CMS

在我的网站和 wordpress 博客之间共享会话

在 2 个 WordPress 上共享用户安装在同一台服务器上的 2 个子域中

在服务于不同域的 apache 虚拟主机之间共享 SSL 配置

TPPHP同域不同子级域名共享Session单点登录

在 php 和 node 之间共享会话