共享数据库的两个WordPress站点多用户及角色同步问题
Great job getting the user sharing and single sign-on working between your WooCommerce store (Site 1) and WordPress blog (Site 2)! The final piece—syncing user roles across both sites—can be tackled cleanly with custom hooks and database-aware code, since you’re already sharing the same database. Here’s how to implement it:
1. Real-Time Role Sync on Role Changes
When a user’s role is updated on either site, we’ll hook into WordPress’s set_user_role action to push those changes to the other site. You’ll need to add this code to the functions.php file of both sites, adjusting the table prefixes to match your setup.
For Site 1 (WooCommerce)
add_action('set_user_role', 'sync_role_to_site2', 10, 3); function sync_role_to_site2($user_id, $new_role, $old_roles) { // Replace 'wp2_' with your Site 2's database table prefix $site2_prefix = 'wp2_'; global $wpdb; // Grab the current site's role data for the user $capabilities = get_user_meta($user_id, $wpdb->prefix . 'capabilities', true); $user_level = get_user_meta($user_id, $wpdb->prefix . 'user_level', true); // Update the role data in Site 2's usermeta table update_user_meta($user_id, $site2_prefix . 'capabilities', $capabilities); update_user_meta($user_id, $site2_prefix . 'user_level', $user_level); }
For Site 2 (WordPress Blog)
add_action('set_user_role', 'sync_role_to_site1', 10, 3); function sync_role_to_site1($user_id, $new_role, $old_roles) { // Replace 'wp_' with your Site 1's database table prefix $site1_prefix = 'wp_'; global $wpdb; // Grab the current site's role data for the user $capabilities = get_user_meta($user_id, $wpdb->prefix . 'capabilities', true); $user_level = get_user_meta($user_id, $wpdb->prefix . 'user_level', true); // Update the role data in Site 1's usermeta table update_user_meta($user_id, $site1_prefix . 'capabilities', $capabilities); update_user_meta($user_id, $site1_prefix . 'user_level', $user_level); }
2. Sync Roles for Newly Registered Users
Make sure new users get their roles synced immediately after signing up on either site. Add this code to both sites’ functions.php as well:
For Site 1
add_action('user_register', 'sync_new_user_role_to_site2'); function sync_new_user_role_to_site2($user_id) { $site2_prefix = 'wp2_'; global $wpdb; $capabilities = get_user_meta($user_id, $wpdb->prefix . 'capabilities', true); $user_level = get_user_meta($user_id, $wpdb->prefix . 'user_level', true); update_user_meta($user_id, $site2_prefix . 'capabilities', $capabilities); update_user_meta($user_id, $site2_prefix . 'user_level', $user_level); }
For Site 2
add_action('user_register', 'sync_new_user_role_to_site1'); function sync_new_user_role_to_site1($user_id) { $site1_prefix = 'wp_'; global $wpdb; $capabilities = get_user_meta($user_id, $wpdb->prefix . 'capabilities', true); $user_level = get_user_meta($user_id, $wpdb->prefix . 'user_level', true); update_user_meta($user_id, $site1_prefix . 'capabilities', $capabilities); update_user_meta($user_id, $site1_prefix . 'user_level', $user_level); }
3. Sync Existing Users’ Roles
If you already have users on either site, you’ll need to run a one-time sync to align their roles across both sites. Add this temporary code to one of your sites’ functions.php, load the site once, then remove or comment it out:
function sync_all_existing_user_roles() { // Replace with the prefix of the site you want to sync TO $target_site_prefix = 'wp2_'; global $wpdb; $users = get_users(); foreach ($users as $user) { $capabilities = get_user_meta($user->ID, $wpdb->prefix . 'capabilities', true); $user_level = get_user_meta($user->ID, $wpdb->prefix . 'user_level', true); update_user_meta($user->ID, $target_site_prefix . 'capabilities', $capabilities); update_user_meta($user->ID, $target_site_prefix . 'user_level', $user_level); } } // Run once, then comment out // sync_all_existing_user_roles();
Key Notes
- Role Consistency: Ensure role names match across both sites (e.g., WooCommerce’s
customerrole should map to whatever equivalent you want on your blog, likesubscriber). If you need role mapping, adjust the$capabilitiesarray before updating the target site’s meta. - Table Prefixes: Double-check that you’re using the correct table prefixes for each site—this is critical for the code to target the right database tables.
- Testing: Test with a test user first to make sure roles sync correctly before rolling out to all users.
内容的提问来源于stack exchange,提问作者Manju Talluri

