Sync users from specific user role

Hi,

I want the users from specific user role to be synced to slave website, is it possible. Can you please let me know how to do it. If any code modification required please let me know in details.

Thank you,
Chandra Mohan

  • Huberson

    Here's some custom code that might help you achieve that:

    add_filter('user_sync_custom_sql', 'user_sync_roles', 999);
    function user_sync_roles($sql) {
        global $wpdb;
    
        $roles = array('administrator','editor');
    
        $sql = "SELECT ID FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id
            WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities'
            AND ( ";
    
        $i = 1;
        foreach($roles as $role) {
            $sql .= "{$wpdb->usermeta}.meta_value LIKE '%{$role}%'";
            if($i < count($roles)) {
                $sql .= " OR ";
            }
            $i++;
        }
        $sql .= " ) ";
    
        return $sql;
    }

    You can create a php file in your site directory under "/wp-content/mu-plugins" and add the code in it. The role you want sync can be added to the roles array inside this portion of the code, separated by commas:
    $roles = array('administrator','editor','subscriber','author');

    Do let us know if you have any question on that .

    Cheers,
    Huberson

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.