Skip to:
Content
Pages
Categories
Search
Top
Bottom

Solved – Need to restrict my users to join multiple groups,


  • Raakesh
    Participant

    @rocky2sdat

    Good day,

    I currently working on my office project. which is a website for managing a hackathon event that occurs every year. Our users will be in teams of 3 people and will be able to post about their project live on our website.
    Last year we did with hackerearth.com but this year our company hire me to build a website. Now I’ve used Buddypress for my website.

    Please find below the brief description for the site.

    1. The users cannot be part of two teams.

    2. Group size should be 3 members. If the group filled with 3 members no other users can able to join in the group.

    3. If a user already joined in a group he can’t able to create a group. If he left that group he can create a group.

    4. If a user created a group he will not able to join a different group or create another group.

    PS: For the second point I’ve used ‘BP Limit Members Per group’ it works fine. But still users can join multiple groups and I’ve also used youzer plugin.

    Please help me to resolve this.

Viewing 25 replies - 1 through 25 (of 33 total)

  • Venutius
    Moderator

    @venutius

    Hi there, another plugin to help you is https://wordpress.org/plugins/buddypress-restrict-group-creation/ this will allow you to restrict members from creating more than one group.

    If I updated this to include a restriction based on the number of group membership then that would help.

    Looking at the code you’d also need to restrict the group join button showing so that it only shows for members not already in a group. How you do this depends on the BP Theme you are using, Legacy or Nouveau.


    Venutius
    Moderator

    @venutius

    I’ve updated restrict group creation to allow limits based on the number of group memberships


    Raakesh
    Participant

    @rocky2sdat

    Hello Venutius,

    Much appreciated, Thanks for the immediate update and help. I’ve used the plugin but it does not works! Please fine the snap shot

    null

    This rule is for contributor part. I’ve used ‘edit_posts’ capability. Please help me to fix this.’

    Thanks,
    Raakesh V.


    Venutius
    Moderator

    @venutius

    Have you deactivated user creation of groups in Settings>>BuddyPress>>Options?


    Raakesh
    Participant

    @rocky2sdat

    Hello @Venutius,

    Oh yes I forget that now its working fine!! But users still able to join multiple groups. Please fine below the snap shot.

    Error1

    Thanks,
    Raakesh V


    Venutius
    Moderator

    @venutius

    Hi Raakesh, glad it’s working for you, could you let me know which BP Theme you are using and Ill see if I can write some code to restrict joining to only one group?


    Raakesh
    Participant

    @rocky2sdat

    Good day Venutius,

    Much appreciated, Please look my website Thinkbox I’ve used Astra WordPress theme and Youzer plugin for rich look.

    Thanks,
    Raakesh V.


    Venutius
    Moderator

    @venutius

    You can see which BP Theme you are using by going to Settings>>BuddyPress>>Options


    Raakesh
    Participant

    @rocky2sdat

    Hello @Venutius,

    I’ve checked Buddypress options it shows Buddypress Legacy template.

    Please find below the snap shot.

    Bptheme


    Venutius
    Moderator

    @venutius

    you can add the following to your child themes functions.php file:

    add_filter( 'bp_get_group_join_button', 'venutius_restrict_group_join', 10, 2 );
    
    function venutius_restrict_group_join( $button, $group ) {
    	global $bp;
    	if ( current_user_can( 'manage_options' ) || $group->is_member ) {
    		return $button;
    	}
    	
    	$membership_total = count( BP_Groups_Member::get_membership_ids_for_user( $bp->loggedin_user->id ) );
    	
    	if ( $membership_total >= 1 ) {
    		return false;
    	}
    	
    	return $button;
    }

    Venutius
    Moderator

    @venutius

    Note that there is a flaw to this, if a user who has no group visits the groups page they will see they can join any group, if they click to join a group the other join buttons will still show until the page is reloaded, I’ll see if I can knock up a filter for this too.


    Venutius
    Moderator

    @venutius

    Hi there, I’ve taken a look at the code and there are no hooks that exist to allow you to prevent a member joining if they were able to press the join group button. What are the timescales for your project? You’d probably need to raise a feature request for this. It’s possible to do, but you’d have to introduce an unofficial filter into the BP source code.


    Raakesh
    Participant

    @rocky2sdat

    Hello @Venutius,

    The filter is working fine. If possible could you please help me with the auto page reload if the user click join group.

    A whole hearted and heart felted Thank you all for your solution’s on my need towards the issues. Definitely this is keeps motivated and to be more active in this community to learn and share the knowledge. Once again Thanks a Ton for All.


    Venutius
    Moderator

    @venutius

    I’ve come up with a workaround, if we can’t stop them joining, after they’ve joined we can undo it:

    add_action( 'groups_join_group', 'venutius_group_join_filter', 10, 2 );
    
    function venutius_group_join_filter( $group_id, $user_id ) {
    	global $bp;
    	
    	if ( current_user_can( 'manage_options' ) ) {
    		return;
    	}
    	
    	$membership_total = count( BP_Groups_Member::get_membership_ids_for_user( $user_id ) );
    	
    	if ( $membership_total >= 2 ) {
    		groups_leave_group( $group_id, $user_id )
    	}
    	
    }

    Raakesh
    Participant

    @rocky2sdat

    Hello @@venutius,

    The above action is undo user to join group. If a user joined in a group it automatically undo it.
    ‘if ( $membership_total >= 1 )’ is not working. Please fix thanks in advance.


    Venutius
    Moderator

    @venutius

    Ah sorry, fixed it.


    Raakesh
    Participant

    @rocky2sdat

    Hello @Venutius,

    Fantastic, you did a wonderful job. Its working 🙂 Thank you so much for your immediate help. Much appreciated. Once again Thanks a Ton for All.


    erbnando
    Participant

    @erbnando

    @venutius Thanks for the code snippets. Exactly what I was looking for!


    restorm
    Participant

    @restorm

    Hello, @venutius: I’m wondering if we can use your plugin to restrict our members to joining a maximum of three groups.


    Venutius
    Moderator

    @venutius

    It’s worth a try,

    Just change $membership_total >= 2 to 4 in the snippet


    restorm
    Participant

    @restorm

    Many thanks for the quick response. I’m not a developer so please excuse two clueless questions:
    1) Do these snippets work on their own, or does your User Roles plugin need to be activated?
    2) Do I insert both of the above snippets in the child theme, like this?
    add_filter( ‘bp_get_group_join_button’, ‘venutius_restrict_group_join’, 10, 2 );

    function venutius_restrict_group_join( $button, $group ) {
    global $bp;
    if ( current_user_can( ‘manage_options’ ) || $group->is_member ) {
    return $button;
    }

    $membership_total = count( BP_Groups_Member::get_membership_ids_for_user( $bp->loggedin_user->id ) );

    if ( $membership_total >= 1 ) {
    return false;
    }

    return $button;
    }
    add_action( ‘groups_join_group’, ‘venutius_group_join_filter’, 10, 2 );

    function venutius_group_join_filter( $group_id, $user_id ) {
    global $bp;

    if ( current_user_can( ‘manage_options’ ) ) {
    return;
    }

    $membership_total = count( BP_Groups_Member::get_membership_ids_for_user( $user_id ) );

    if ( $membership_total >= 4 ) {
    groups_leave_group( $group_id, $user_id )
    }

    }


    Venutius
    Moderator

    @venutius

    You just need the latest snippet, only use the one.

    You don’t need any other plugins apart from BuddyPress.

    You can put the Snippet into your functions.php

    functions.php


    restorm
    Participant

    @restorm

    Much appreciated!


    restorm
    Participant

    @restorm

    Hi, @venutius. I hate to impose on your generosity, but I’m getting desperate, and you seem like someone who might be able to solve a major problem for us.

    We need to capture the BuddyPress group a user belongs to when they are not on the group page. Do you know where in the BuddyPress user profile that data is stored?

    Also, using your snippet: if we wanted to restrict users to being in just one group, we would set $membership_total >= 2, right?

    Thanks again for your help!


    restorm
    Participant

    @restorm

    Even better would be if that data is in the WordPress user meta data. We’re trying to access it via a hidden field in a Formidable Forms survey.

Viewing 25 replies - 1 through 25 (of 33 total)
  • You must be logged in to reply to this topic.
Skip to toolbar