好吧 - 你已经很接近了,但还有一些事情要做。
所有要求如下:
- 使用不同的前缀共享同一个数据库 - 您已经做到了。从这里我假设前缀是
wp1_
, wp2_
等等。
- 分享
wp1_users
and wp1_usermeta
表 - 你已经做到了这一点 - 实际上,如果你正确拼写了常量名称,你就可以克服这个障碍......CUSTOM_USER_META_TABLE
(比你所拥有的多一个下划线)
- 使用通用方法在子域之间共享 cookie
COOKIE_DOMAIN
and COOKIEHASH
- 你已经做到了
- 确保(通常)随机生成的密钥和盐也是相同的 - 您不会写下您已经完成了此操作,但根据您的结果判断,我认为您要么拥有密钥,要么您的密钥为空(这不好,但会起作用)
- 确保有一个
prefix_capabilities
共享中每个站点的每个用户的条目usermeta
table - 我认为您还没有这样做,只是因为您还没有达到意识到有必要这样做的程度。
完整的解决方案:
这进入wp-config.php
:
//Share user tables
define('CUSTOM_USER_META_TABLE', 'wp1_usermeta');
define('CUSTOM_USER_TABLE', 'wp1_users');
//Share cookies
define('COOKIE_DOMAIN', '.abc.com');
define('COOKIEHASH', 'aee53c017c29dc0d3ae37253fc8cbfd8');
/**
* In my case these are not needed - but they may well be if one
* of the installs is located in a sub-folder. I have not tested
* this theory though.
*/
//define('COOKIEPATH', '/');
//define('SITECOOKIEPATH', '/');
//define('ADMIN_COOKIE_PATH', '/wp-admin');
//These all need to be identical
define('AUTH_KEY', 'this should be random');
define('SECURE_AUTH_KEY', 'this should also be a random string');
define('LOGGED_IN_KEY', 'one more random string');
define('AUTH_SALT', 'oh my - so many random strings');
define('SECURE_AUTH_SALT', 'salt, salt, salt and no eggs');
define('LOGGED_IN_SALT', 'this is sooooo random');
/**
* These do not need to be shared - in fact they probably shouldn't be
* - if they are you could (in theory) do actions on one site that was
* intended for the other - probably not a very big concern in reality
*/
define('NONCE_KEY', 'these should be random too, but can differ');
define('NONCE_SALT', 'one site has one, the other another');
这足以让您登录两个站点 - 但列表中仍然剩下最后一个烦人的项目符号。
问题是您的权限(“能力”)仅在其中一个站点上有效,因为meta_key
以站点的表前缀为前缀。如果你谷歌一下,你会发现很多建议修改的解决方案wp-includes/capabilities.php
只使用通用前缀 - 我强烈建议不要这样做! (不是出于安全原因,而是因为您需要在每次更新后制作此补丁/破解......并且修改核心文件是非常糟糕的做法)
相反,要解决这个障碍,您需要复制wp1_capabilities
行中wp1_usermeta
表(对于每个用户!),给它一个新的umeta_id
并替换表前缀wp1_
with wp2_
in the meta_key
柱子。您需要为每个站点执行此操作,因此您有一行meta_key
wp1_capabilities
,一与wp2_capabilities
等等。
如果您和您的朋友是唯一登录该网站的用户,那么只需通过 phpMyAdmin 或其他东西手动完成即可 - 如果您需要它动态工作,那么应该很可能使用小插件(见下面的编辑)。
我一直讨厌这种设计 - 表前缀在表行中没有任何作用!我认为多站点安装需要它,尽管我确信会有其他(更好)的方法来解决它......
更新:用于保持所有站点之间用户角色同步的插件
这个简单的插件将复制并保留所需的行usermeta
创建或编辑用户时更新表。
值得注意的一件事是,它可能不适用于多站点安装,因为它们具有一些特殊的功能/角色。我没有测试过这个。
它可能需要针对特定用例进行细化(请发表评论),但对于我仅包含少数用户的有限测试用例来说,它可以很好地完成工作。对于拥有数千个用户的网站来说,这将是低效的,但由于它仅在修改用户时运行,并且仅在需要时才进行更新,我怀疑这将是一个主要问题。然而,它应该相对容易适应仅读取和修改刚刚添加/编辑的用户。但这会使初始设置变得有点复杂,因为预先存在的用户不会在第一次运行时自动重复。
创建文件夹wp-content/plugins/duplicate-caps
并在里面放入以下内容duplicate-caps.php
- 并且不要忘记在下面激活plugins
在 WordPress 管理中。它需要安装在所有站点上。
<?php
/*
Plugin Name: Duplicate Caps
Plugin URI:
Description: Tiny plugin to duplicate capabilities in a setup where users (and user tables) are shared across more than one install
Author: Mikk3lRo
Version: 0.1
Author URI:
*/
$dummy = new duplicate_caps();
class duplicate_caps {
function __construct() {
add_action('updated_user_meta', array($this, 'update_prefixed_caps'), 10, 2);
add_action('added_user_meta', array($this, 'update_prefixed_caps'), 10, 2);
add_action('deleted_user_meta', array($this, 'update_prefixed_caps'), 10, 2);
}
function update_prefixed_caps($mid, $object_id) {
/**
* Note that $object_id contains the id of the user that was
* just changed.
* On a site with many users it would make sense to only
* get and set information regarding the just-changed user
* Currently this function corrects roles for all users
* making sure pre-existing users are duplicated, and keeping
* the table in sync.
*/
global $wpdb;
//Quick and dirty - get all *capabilities rows for all users
$sql = "SELECT * FROM {$wpdb->usermeta} WHERE `meta_key` LIKE '%capabilities'";
$results = $wpdb->get_results($sql) or die(mysql_error());
//Will hold all prefixes (always include our own)
$prefixes = array($wpdb->prefix);
//Will grab the existing role for each prefix
$user_roles = array();
//Loop our results
foreach ($results as $result) {
//Make sure the meta_key looks right, and grab the prefix
if (preg_match('#^(.*)capabilities$#', $result->meta_key, $matches)) {
$prefix = $matches[1];
// Collect prefixes
$prefixes[] = $prefix;
//Note the entire row for later use
$user_roles[$result->user_id][$prefix] = $result;
}
}
//Make sure we only have one of each
$prefixes = array_unique($prefixes);
//Loop through the users we found
foreach ($user_roles as $user_id => $existing_prefixes) {
if (!isset($existing_prefixes[$wpdb->prefix])) {
//User was probably just deleted - all rows are deleted by
//wordpress though, so no cleanup for us :)
} else {
//We want all prefixes to obey us (we just created or changed
//the user, so we want that to affect all sites)
$desired_role = $existing_prefixes[$wpdb->prefix]->meta_value;
//Loop through all prefixes
foreach ($prefixes as $prefix) {
//Data to be inserted / updated
$cap_data = array(
'user_id' => $user_id,
'meta_key' => $prefix . 'capabilities',
'meta_value' => $desired_role
);
//If the prefix doesn't exist for this user
if (!in_array($prefix, array_keys($existing_prefixes))) {
//Actually insert it (user was probably just created)
$wpdb->insert($wpdb->usermeta, $cap_data, array('%d', '%s', '%s'));
} else if ($desired_role !== $existing_prefixes[$prefix]->meta_value) {
//Update it if not already correct (user was probably just edited)
$cap_data['umeta_id'] = $existing_prefixes[$prefix]->umeta_id;
$wpdb->replace($wpdb->usermeta, $cap_data, array('%d', '%s', '%s', '%d'));
}
}
}
}
}
}