Is there a way to let User Sync create new users on slave site using the default role

I use User Sync to add new user to all sites I have so that my client could use the same username and password on all of them.
But each site is defined with a different default user role as required by the theme on each one.
Is there a way to configure User Sync to add new user accounts on each site and set the role, based on the default user role defined on the site ?

  • Sajid

    Hi Eric,

    Hope you are doing good and welcome back :slight_smile:

    Unfortunately User Synchronization and Multisite User Management both are for multi sites only.

    For single site import export I suggest you following free plugins. They do have paid upgrades if you wish.

    https://wordpress.org/plugins/wp-ultimate-csv-importer/
    https://wordpress.org/plugins/wp-all-import/
    https://wordpress.org/plugins/cimy-user-manager/

    Hope this will help :slight_smile:

    Cheers, Sajid

  • Eric

    I am really surprised by this answer... check your web site: https://premium.wpmudev.org/project/wordpress-user-synchronization/

    The plugin "User Synchronization" sync user accounts and transfer profiles across multiple WordPress installations.
    User Synchronization links user profiles across multiple single WordPress installations.
    Create and manage user accounts in one place that can be used on any connected site–without Multisite.

    So, why are you saying that this plugin only work on WPMU ?

  • Sajid

    Hi Eric,

    I am sorry for confusion. I am new here and still learning how each plugin work. Sorry again :slight_frown:

    Yes you can use "User Synchronization" plugin for single WordPress installation as well.

    For user synchronization I could not find any plugin similar to "User Synchronization" and that will work for single site installation as well.

    However I found this plugin that will export users into CSV and import them in another WordPress installation. Using this plugin you can set default role as well, that work trying. Here is the link:
    https://wordpress.org/plugins/import-users-from-csv-with-meta/screenshots/

    Have a great weekend :slight_smile:

    Cheers, Sajid

  • Eric

    Hi Sajid,
    thanks for the suggestion but it is not what I am looking for.

    I bought User Sync to synchronize each of my sites and I was expecting that your plugin would;
    1) respect the default role set on each single site
    OR
    2) provide the ability to set different role on different site during the synchronization.

    As new users added to my master site are assigned the role of Customer (role from WooCommerce), currently the role does not get set on other sites as I do not have WooCommerce on those other sites and that force me to set the role manually on each of them, each time a new user is added.

    Do you have another suggestion for me to get this working properly?

  • Sajid

    Hi Eric,

    You are welcome and hope you had a great weekend :slight_smile:

    The "User Synchronization" plugin import and export all data associated with each user to all of your slave/connected sites.

    For your problem I am going to ping SLS (Second Level Support) staff for their valuable feed back on this matter. Please bear in mind that their response will be slower than normal support. Because they resolve technical and advanced level of questions.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Eric

    Hi Jose,
    I have more info that can probably explain, the issue with the fact that the role is not set on my slave.

    When I first installed User Sync, the default role on the master was set to Customer from WooCommerce.

    As I do not have that role set on my slave site, the role was left blank (not set). So, I went back to the master site and I have changed the default role on it to be set as "Subscriber".

    Then, all new users on my slave site continued to be set as blank (not set).

    I have then decided to syn the users with another slave site which had WooCommerce installed on it and surprised ! All users got added as "Customer". As if the Master was still setting using the role "Master" rather than the new default setting which is "Subscriber".

    So, that look to me as a database issue with the User Sync setting.

    Is there a way to correct this in the database or somehow ?
    I would rather not have to de-install the plugin on my master and clear the db but if it is the only solution, I will do :slight_frown:

    Thanks for your help,
    Eric

    See ticket #873244 (https://premium.wpmudev.org/forums/topic/user-role-not-set-with-the-user-synch-plugin#post-873244)

  • jamie

    Will the user_register hook work to allow me to set the user role with and add_action?

    Perhaps that is easiest solution for now?

    Edit: on second thought, this may not reflect any changes set at the master, since the update role does not seem to make changes on sync. Not ideal solution, but since in the future all accounts will register through the master site, at least I can set a default user role other than NONE.

  • Eric

    Hi Jamie,
    which hook have you found to force the role to be set properly each time a change occurs on your slave ?
    On my side, I have done further investigation and it appears that the role on my slaves get set properly ONLY if I have Woocommerce installed on my slave since it is installed on my MASTER.

    Please note that the default role on my MASTER is NOT using a specific role from Woocommerce. It is using the role of Subscriber and on Slaves where I have WooCommerce installed, the role get set properly. On other slaves, each time I modify something on a user account, the role get reset to "blank" not set.

    See also the following discussions:
    https://premium.wpmudev.org/forums/topic/user-role-not-set-with-the-user-synch-plugin
    https://premium.wpmudev.org/forums/topic/problem-password-change-not-sync-on-slave

  • Sajid

    Hi @Eri! Hope you are doing good today :slight_smile:

    Jamie is asking for a hook to set user role on registration. Yes! We can use "user_register" hook to set default role on user registration. Add below code in functions.php file of your child theme or use as mu-plugin.

    //
    // Modify registration form to include roles
    //
    add_action('user_register', 'register_role');
    function register_role($user_id, $password="", $meta=array()) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = 'subscriber';
    
       //only allow if user role is my_role
       if (($userdata['role'] == "subscriber") ){
          wp_update_user($userdata);
       }
    }

    Note in above code snippet it will always set "subscriber" role regardless of role in master site. You can replace "subscriber" (2 instances in above code) with your own role e.g. "customer".

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Jose

    Hello there @Eric and @jamie,

    It seems that all the issues were caused by the same bug related to DB prefixes.

    I included a fix for this in the beta version attached in this post.

    Also, I added a handy hook that will allow to easily override the role assigned to the user.

    This is an example of how to use it (it can be added into functions.php or mu-plugins):

    function user_sync_filter_capabilities($capabilities, $userdata, $user_id){
        if( $user_id > 1 ){
            $default_role = get_blog_option( null, 'default_role');
            $capabilities[$default_role] = 1;
        }
        return $capabilities;
    }
    
    add_filter( 'user_sync_user_capabilities', 'user_sync_filter_capabilities', 10, 3 );

    In the snippet above, we are using the value of 'New User Default Role' as a fallback for when the role is not sent in the synchronization data or either when the role sent is not defined in the slave site.

    Please give it a try and let me know if I missed something.

    Cheers,
    Jose

      • Jose

        Hey Jamie,

        It is not so complicated. Actually, all the fixes are quite simple once you spot the bug.

        In the method _get_user_data() I added the following lines:

        //Make sure that the default 'wp_capabilities' key is fulfilled so that slave sites knows where to look for roles data.
        $capabilities_key = $wpdb->get_blog_prefix() . 'capabilities';
         $user_meta['wp_capabilities'] = $user_meta[$capabilities_key];

        As the comment explains, this is to ensure that we allocate the roles data in the default key 'wp_capabilities' even when the prefix is different.

        Then, when we read the data on the slave site side, we need to look for 'wp_capabilities', but make sure that we store the user metadata with the correct prefix.
        So, I changed the role routine into update_other_user_data() to look like this:

        //Update user Role
        $result = $wpdb->query( $wpdb->prepare( "
                    UPDATE {$wpdb->base_prefix}usermeta SET
                    meta_value = '%s' WHERE user_id = '%d' AND meta_key = '%s'",
                    serialize( apply_filters( 'user_sync_user_capabilities', $userdata['wp_capabilities'], $userdata, $user_id ) ), $user_id, $wpdb->get_blog_prefix() . 'capabilities' ) );
        unset($userdata['wp_capabilities']);

        You can see that I'm applying the filter there so that we can hook externally.

        Let me know if you have any other question. :slight_smile:

        Cheers,
        Jose

  • Eric

    Hi Jose,
    does it mean that if I install this new version of the plugin AND I use the hook you have provided, all roles on my Slave sites will be set to the default role define on the slave sites ?

    What if I modify the role on one of the user and that user account is modified on the Master, will the role on the slave side be reset to the default role or it is just at creation time ?

    Thanks.. I will install that on my test server and give it a try tomorrow.
    /Eric

    • Jose

      Hi Eric,

      does it mean that if I install this new version of the plugin AND I use the hook you have provided, all roles on my Slave sites will be set to the default role define on the slave sites ?

      Yes, as long as the user role in master site doesn't match any existing role in slave site.
      If the role set in master site exists in slave site, it will take precedence. Otherwise it will behave as you describe above.

      What if I modify the role on one of the user and that user account is modified on the Master, will the role on the slave side be reset to the default role or it is just at creation time ?

      The expected behaviour is that the role will be reset again. The syncing is triggered not only at creation time but also when the profile is edited.

      Please let me know if it works as expected now.
      If there are no issues, I can push this fixes to be included in future releases. :slight_smile:

      Cheers,
      Jose

  • Eric

    RESULTS OF MY TESTING

    Hi Jose and Jamie,
    I have done many tests this morning and the fix seems to work fine. The only thing that I found strange is that I had to add the hook you have provided above (to preserve the role) to the function.php file for only one of my slave site. For the other one, I have NOT added the hook and everything was fine. I could add or modify user accounts and the role was alway preserved.

    And just in case you are interested, the MASTER has "wp_" as prefix, the slave (without the hook) has "wpbo_" as prefix and the slave that work ONLY if the hook is available in the function.php has "wp_" has db prefix.

    So, in conclusion... the fix is working BUT not always ! But I could not find why the hook is required on one slave and not the other one. Really strange. So, I will use the hook all the time now.

    And one more note... do not forget to remove line 369 ('Custom_Debugger') from your file user-sync.php before releasing as that generate an error for me :slight_smile:

    Great work...thanks
    /Eric

      • Eric

        Hi Jose,
        Yes... for sure the role exists on the slave as I made sure to pick a generic role. But with the hook, that seems to solve the issue. What I do not get is that it is only for one of the slave sites... not all !

        This being said... I have another issue - Reinitializing the password does not get propagated

        If, via the user interface, I modify the password from the profile screen of the user... no problem... that works perfectly. BUT, if i click on "Forgot password", which generate an email to the user to click on a link to set a new password, the password is only modified on the MASTER and is not propagated to all slaves.

        Is it possible that it is a different function call that is not handled properly ?

  • Phil Adie

    Gentlemen,

    I have the exact opposite problem from Eric. I have several regional sites all with Custom Roles ie Ottawa Sales, Ontario Service, etc. I opened a internal store for uniforms with WooCommerce.

    So I have my regional sites as Masters and store site as Slave. When I try sync everything comes in ok except Role which defaults to Customer. I need to have the 3 distinct User Roles import in intact and not default to Customer.

    Any guidance would be appreciated.

    Thanks,

    Phil