最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成
假设我们有两个网站,在你的问题中提到-abc.com, xyz.abc.com, 其表前缀为-ab_, xy_.
要求两个网站必须安装在同一个域中。两个网站必须使用不同的表前缀共享相同的数据库。两个网站必须共享用户表(例如。ab_users, ab_usermeta ).
网站的wp配置。php文件wp-config.php 两个网站的文件应该相同,只有一个例外,$table_prefix 对于abc.com 网站应为ab_, 对于xyz.abc.com 应该是xy_. 看见wp-config.php 对于abc.com, 以下内容:
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don\'t have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'abc\'); // change it, to match your installation
/** MySQL database username */
define(\'DB_USER\', \'abcadmin\'); // change it, to match your installation
/** MySQL database password */
define(\'DB_PASSWORD\', \'database pasword here\'); // change it, to match your installation
/** MySQL hostname */
define(\'DB_HOST\', \'localhost\'); // change it, to match your installation
/** Database Charset to use in creating database tables. */
define(\'DB_CHARSET\', \'utf8\');
/** The Database Collate type. Don\'t change this if in doubt. */
define(\'DB_COLLATE\', \'\');
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define(\'AUTH_KEY\', \'use generated value here\');
define(\'SECURE_AUTH_KEY\', \'use generated value here\');
define(\'LOGGED_IN_KEY\', \'use generated value here\');
define(\'NONCE_KEY\', \'use generated value here\');
define(\'AUTH_SALT\', \'use generated value here\');
define(\'SECURE_AUTH_SALT\', \'use generated value here\');
define(\'LOGGED_IN_SALT\', \'use generated value here\');
define(\'NONCE_SALT\', \'use generated value here\');
define(\'COOKIE_DOMAIN\', \'.abc.com\');
define(\'COOKIEPATH\', \'/\');
define(\'COOKIEHASH\', md5(\'abc.com\'));
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = \'ab_\'; // in wp-config.php for xyz.abc.com change it to \'xy_\'
/* uncomment these two lines after successful website installation */
// define(\'CUSTOM_USER_TABLE\', \'ab_users\');
// define(\'CUSTOM_USER_META_TABLE\', \'ab_usermeta\');
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define(\'WP_DEBUG\', false);
/* That\'s all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
新建安装创建一个空数据库供两个安装使用。
滴wp-config.php 对于abc.com 到的根abc.com 并进行安装。暂时不要登录您的网站。写下管理员的用户名和密码。
滴wp-config.php 对于xyz.abc.com 到的根xyz.abc.com 并进行安装。请勿登录新网站。
在两个网站中,创建mu-plugins 文件夹位于/wp-content, 如果它不存在。
制作fpw-sync-users.php 包含以下内容的文件:
<?php
/*
Plugin Name: FPW Synchronize Shared Users
Author: Frank P. Walentynowicz
Author URI: https://fw2s.com
Disclaimer: Use at your own risk. No warranty expressed or implied is provided.
*/
// Users synchronization on admin login
function fpw_synchronize_admins_on_admin_login( $user_login, $user ) {
if ( array_key_exists( \'administrator\', $user->caps ) ) {
global $wpdb;
$site_prefix = $wpdb->prefix;
$admins_only = true;
$other_prefixes = array(
\'xy_\',
);
$args = array(
\'fields\' => \'ID\',
);
if ( $admins_only )
$args[ \'role\' ] = \'administrator\';
$users = get_users( $args );
foreach ( $users as $id ) {
$cap = get_user_meta( $id, $site_prefix . \'capabilities\', true );
foreach ( $other_prefixes as $prefix )
update_user_meta( $id, $prefix . \'capabilities\', $cap );
}
}
}
add_action( \'wp_login\', \'fpw_synchronize_admins_on_admin_login\', 10, 2 );
// User synchronization on admin create user
function fpw_synchronize_user_on_admin_register( $id ) {
$me = wp_get_current_user();
if ( array_key_exists( \'administrator\', $me->caps ) ) {
$other_prefixes = array(
\'xy_\',
);
$user = get_user_by( \'id\', $id );
$cap = $user->caps;
foreach ( $other_prefixes as $prefix )
update_user_meta( $id, $prefix . \'capabilities\', $cap );
}
}
add_action( \'user_register\', \'fpw_synchronize_user_on_admin_register\', 10, 1 );
// User synchronization on profile update
function fpw_synchronize_user_on_profile_update( $user_id ) {
if ( current_user_can( \'edit_user\', $user_id ) ) {
$other_prefixes = array(
\'xy_\',
);
$cap = array( $_POST[ \'role\' ] => true, );
foreach ( $other_prefixes as $prefix )
update_user_meta( $user_id, $prefix . \'capabilities\', $cap );
}
}
add_action(\'edit_user_profile_update\', \'fpw_synchronize_user_on_profile_update\');
放下
fpw-sync-users.php 文件收件人
/wp-content/mu-plugins 两个网站的文件夹。
修改fpw-sync-users.php 的文件xyz.abc.com, 通过替换每次出现的:
$other_prefixes = array(
\'xy_\',
);
使用:
$other_prefixes = array(
\'ab_\',
);
修改
wp-config.php 两个网站的文件,通过取消注释这两个定义:
// define(\'CUSTOM_USER_TABLE\', \'ab_users\');
// define(\'CUSTOM_USER_META_TABLE\', \'ab_usermeta\');
全部完成。登录到
abc.com 然后转到
xyz.abc.com. 您也将登录到此网站。
你可以放弃xy_users 和xy_usermeta 数据库中的表,因为它们将不再使用。
当我们使用单独的数据库处理现有的网站时,现有的安装变得稍微复杂一些。
Important: 备份wp config。继续之前,请访问两个网站的php文件和数据库!
让我们使用abc.com 网站作为共享数据库。我们必须导出所有表(除了users 和usermeta ) 从…起xyz.abc.com 数据库,并将其导入abc.com 数据库
确保表前缀xyz.abc.com 数据库与的表前缀不同abc.com 数据库如果不同,可以跳过以下更改xyz表格前缀的过程。美国广播公司。com。
Change table prefix for xyz.abc.com: 安装并激活WP Prefix Changer 插件。运行其过程以更改前缀。停用并删除此插件。现在,您可以导出/导入表了。
从导出表xyz.abc.com 数据库,并将其导入abc.com 数据库为此,您可以使用phpMyAdmin, 或任何其他可用工具。
修改wp-config.php (参见wp-config.php 示例来自New installations 第)节abc.com, 通过添加以下定义:
define(\'COOKIE_DOMAIN\', \'.abc.com\');
define(\'COOKIEPATH\', \'/\');
define(\'COOKIEHASH\', md5(\'abc.com\'));
以及
define(\'CUSTOM_USER_TABLE\', \'ab_users\');
define(\'CUSTOM_USER_META_TABLE\', \'ab_usermeta\');
修改
wp-config.php 对于
xyz.abc.com, 通过替换与数据库相关的定义,以匹配共享数据库值。添加定义,您刚刚添加到
wp-config.php 属于
abc.com. 替换键和散列定义,以匹配中的这些定义
wp-config.php 对于
abc.com.
添加(如中所述New installations 第节)fpw-sync-users.php 同步插件到/wp-content/mu-plugins 并相应修改其前缀。
就是这样。现在您有了可以使用SSO的共享用户。