1

I am working on creating a Woocommerce Store (SITE 1) and a Wordpress Blog (SITE 2) that will be accessed by the same set of users.

To make it convenient for the users both the networks will be sharing users and logins.

I have managed to acheive 90% of the result with the help of these posts

https://kinsta.com/blog/share-logins-wordpress/ and

https://trickspanda.com/wordpress-share-users-login/

So I have successfuly completed

1) SHARING USERS AND USER META between SITE 1 and SITE 2

2) SHARING LOGIN COOKIES between SITE 1 and SITE 2

So all is fine till here.

Now 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 = 'SITE1_'; // SITE 1's Table Prefix
    $prefix_2 = 'SITE2_'; // SITE 2's Table Prefix

    $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 "SITE1_capabilities" to "SITE2_capabilities"

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

enter image description here

If we can copy the entire meta_value from “SITE1_capabilities” to “SITE2_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!

0 Answers0