好的 - 你已经很接近了,但还有一些事情要做。
所有要求如下:
- 使用不同的前缀共享同一个数据库 - 你已经做到了。从这里我假设前缀是
wp1_、wp2_等等。
- 分享
wp1_users 和wp1_usermeta 表 - 你已经这样做了 - 实际上,如果你正确拼写常量名称,你就会克服这个障碍......它是CUSTOM_USER_META_TABLE(比什么多一个下划线你有)
- 使用共同的
COOKIE_DOMAIN 和 COOKIEHASH 在子域之间共享 cookie - 你已经做到了
- 确保(通常)随机生成的密钥和盐也是相同的 - 你没有写你已经这样做了,但从你的结果来看,我认为你有或者你的密钥是空的(这是不好,但会起作用)
- 确保共享
usermeta 表中每个站点的每个用户都有一个 prefix_capabilities 条目 - 我认为您没有这样做,只是因为您还没有达到您意识到它的地步必要的。
完整解决方案:
这在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_usermeta 表中的wp1_capabilities 行(针对每个用户!),给它一个新的umeta_id 并将表前缀wp1_ 替换为wp2_ meta_key 列。您需要为每个站点执行此操作,因此您有一排是 meta_key wp1_capabilities,一排是 wp2_capabilities,依此类推。
如果您和您的朋友是唯一会登录网站的用户,那么只需通过 phpMyAdmin 或其他方式手动完成 - 如果您需要它动态工作,那么应该很有可能用一个小插件自动化(见下面的编辑)。
我一直讨厌这种设计 - 表格前缀在表格行中没有任何意义!我认为多站点安装需要它,但我确信会有其他(更好的)方法来解决它......
更新:使用户角色在所有站点之间保持同步的插件
这个简单的插件将在创建或编辑用户时复制并保持usermeta 表中所需的行更新。
值得注意的是,它可能不适用于多站点安装,因为它们具有一些特殊的功能/角色。我没有测试过。
它可能需要针对特定用例进行改进(请发表评论),但对于我只包含少数用户的有限测试用例,它可以很好地完成工作。对于拥有数千名用户的站点来说,这将是低效的,但由于它仅在用户被修改时运行,并且仅在需要时才进行更新,我怀疑这将是一个主要问题。然而,它应该相对容易适应只读取和修改刚刚添加/编辑的用户。不过,这会使初始设置复杂一些,因为预先存在的用户不会在第一次运行时自动复制。
创建文件夹wp-content/plugins/duplicate-caps 并将以下内容放入duplicate-caps.php - 并且不要忘记在wordpress 管理员中的plugins 下激活。它需要安装在所有站点上。
<?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'));
}
}
}
}
}
}