Using the synchronization plugin with the groups plugin

I have exactly the same requirement as the user in this post: https://premium.wpmudev.org/forums/topic/can-i-filter-specific-user-roles-to-synchronize. The solution given requires the lines 407-409 to be replaced in user-sync.php however the code at lines 407-409 appears to have changed since this was written.

Could someone let me know where this code should be placed or if there is an alternative solution to syncing users and their respective groups?

Thanks

  • Predrag Dubajic

    Hey @Daniel Maby,

    Hope you're doing well today :slight_smile:

    I had a look at the code and it looks like there weren't any changes to that file since then and the code is still on lines 407-409, what you need to do is replace this part of code:

    foreach( $rows as $row ) {
        $result[] = $row->ID;
    }

    With this:

    //= First create a Group object - set $group to the name of the group
    $group = 'group_name';
    $current_group = Groups_Group::read( $group );
    
    foreach( $rows as $row ) {
    	//= If the user in the group then return them add them to the result
    	if ( Groups_User_Group::read( $groups_user->user->ID , $current_group->group_id ) ) {
    		$result[] = $row->ID;
    	}
    }

    Below is the screenshot of the part of code that needs replacing.

    Hope this helps.

    Best regards,
    Predrag

  • Daniel Maby

    Hey @Predrag Dubajic

    Thanks for the quick response! This is a bit odd then because when I look at lines 407-409 I see the following:

    if ( isset( $_POST['usync_action'] ) )
    
                switch( $_POST['usync_action'] ) {

    The code you referred to is on lines 813-815? Please see attached. I had originally tried replacing lines 813-815 with the code but it didn't sync. I'm working in wp-content/plugins/user-sync/user-sync.php on the Master Site.

  • Predrag Dubajic

    Hey Daniel,

    I see what confused you, there are simply empty lines added between causing the file to have twice as much lines, this usually happens when file is being created on Mac and then edited on Windows, or other way around, but it's nothing to be worried about.

    The correct lines in your case are the ones you highlighted on your screenshot.
    You mentioned above that you changed 813-815 and in that case you had one more closing bracket than needed so that may be why it's not working.

    Could you try it again but this time replace 813-817 and let us know how it goes.

    Best regards,
    Predrag

  • Predrag Dubajic

    Hi Daniel,

    Have you changed group_name in the code to your desired group name?

    Also, please note that this code is not provided by the WMPU staff member and it has not been tested so there are no guarantees that it will work.

    If it's not working after changing group name could you please write in detail what are you looking to accomplish so we can start looking for a different solution.

    Best regards,
    Predrag

  • Daniel Maby

    Thanks again Predrag, I have changed the group_name to the one I'm trying to sync but still no joy.

    I have users registered in the Master site; example.com I am using a combination of Groups and WooCommerce to create a subscription based platform. The intention is to create an additional Slave site at dashboard.example.com allowing users of the master site to gain access to specific pages within the Slave site based on the Group they are in, in the Master site.

  • Adam Czajczyk

    Hey Daniel,

    I hope you're well today! Would you mind me jumping into the conversation for a while?

    I tried to check this code on my own test setup and also replicate a case at least similar to yours. I've got a question though:

    do you also use Membership Pro (as does the member from the thread that you mentioned) or do you mean "Buddy Press Groups" by "Groups"? Or is it some other plugin? Please advise!

    Cheers,
    Adam

  • Adam Czajczyk

    Hey Daniel,

    Thanks for this info!

    I've downloaded the Groups plugin and examined it and here's what I've come up with.

    Remember the code from @Predrag Dubajic replay (here). This code is good but it's not specific to this particular plugin.

    As you can see the code resides inside the "get_all_users_id()" functions. To make it work with Groups plugin please replace this entire function with the following one:

    /**
         *  Get all Users ID
         **/
        function get_all_users_id() {
            global $wpdb;
            $mygroup = 2; // this is a group ID
    		$tb_users = $wpdb->base_prefix."users";
    		$tb_groups = $wpdb->base_prefix."groups_user_group";
    
    		$myquery = "SELECT ".$tb_users.".ID FROM ".$tb_users." WHERE ".$tb_users.".ID in (SELECT ".$tb_groups.".user_id from ".$tb_groups." WHERE ".$tb_groups.".group_id = 2)";
    		$rows = $wpdb->get_results($myquery);
    
            foreach( $rows as $row ) {
                $result[] = $row->ID;
            }
            return $result;
        }

    It works as expected on my test setup so I hope it will also work fine for you. Of course, please make a backup copy of the original user-sync.php file first, just in case :slight_smile:

    Also, value of the "$mygroup" variable is an ID of the group. You can find it in your dashboard on a "Groups" -> "Groups" page.

    Let me know if it worked!

    Cheers,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.