Return to Snippet

Revision: 49914
at August 2, 2011 06:34 by lancemonotone


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

Revision: 49913
at August 2, 2011 06:34 by lancemonotone


Initial Code
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');`

Initial URL
http://wordpress.org/support/topic/wordpress-25-share-users-table#post-904597

Initial Description
There are a few modifications to make to your WP files to share a single database between multiple installs on separate domains.

Initial Title
Wordpress: Share database between different domains

Initial Tags
wordpress

Initial Language
PHP