Sync all User Roles between two Wordpress Installs sharing the same wp_users and wp_usermeta tables.

北慕城南 提交于 2019-11-28 09:17:16

问题


I have created a Woocommerce Store (database prefix wp_) and a Wordpress Blog (database prefix wp_new_) that share the same users (sharing the same wp_users and wp_user_meta tables).

I want to sync not just users but also user roles (multiple user roles) of all users.

For this I tried the solution offered by https://kinsta.com/blog/share-logins-wordpress/

    function ksu_save_role( $user_id, $role ) {

        $prefix_1 = 'wp_'; 
        $prefix_2 = 'wp_new_'; 

        $caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
        $level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );

        if ( $caps ){
            update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
        }

        if ( $level ){
            update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
        }
    }
add_action( 'set_user_role', 'ksu_save_role', 10, 2 );

The above solution works great when only a single user role is assigned to a user. But if a user is assigned multiple user roles then it doesn’t work. I mean, it doesn’t sync all user roles.

After digging through the database I understood that the solution lies in cloning the "meta_value" of "wp_capabilities" to "wp_new_capabilities" (in wp_usermeta)

Is there a way to copy entire ‘meta_value’ for a ‘user_id’ from “wp_capabilities” to “wp_new_capabilities”?

If we can copy the entire meta_value from “wp_capabilities” to “wp_new_capabilities” then all the user roles assigned to a user can be synced.

So what changes need to be done to the above mentioned code to achieve this?

Thanks!


回答1:


I had issues with syncing user roles (multiple user roles per user). After burning midnight oil for more than two nights I found the silly magical solution :)

I simply changed 'set_user_role' to 'add_user_role' in "add_action( 'set_user_role', 'ksu_save_role', 10, 2 );"

The end code after the small magical tweak

function ksu_save_role( $user_id, $role ) {

    // Site 1
    // Change value if needed
    $prefix_1 = 'first_';

    // Site 2 prefix
    // Change value if needed
    $prefix_2 = 'second_';

    $caps = get_user_meta( $user_id, $prefix_1 . 'capabilities', true );
    $level = get_user_meta( $user_id, $prefix_1 . 'user_level', true );

    if ( $caps ){
        update_user_meta( $user_id, $prefix_2 . 'capabilities', $caps );
    }

    if ( $level ){
        update_user_meta( $user_id, $prefix_2 . 'user_level', $level );
    }
}

add_action( 'add_user_role', 'ksu_save_role', 10, 2 ); // THE MAGIC MODIFICATION

Code credits: https://kinsta.com/blog/share-logins-wordpress/

Add this to functions.php and you are great to go.

It is compatible with role changer plugins like "Woocommerce Subscriptions" and "YITH Automatic Role Changer for WooCommerce Premium"

You can set and change as many roles as you want.



来源:https://stackoverflow.com/questions/49279891/sync-all-user-roles-between-two-wordpress-installs-sharing-the-same-wp-users-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!