Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to restrict group creation to certain users


  • itsasiak
    Participant

    @itsasiak

    Hi all,

    This “trick” is intended to allow certain WP user roles (like Admin or Subscriber) to create groups while preventing the rest, and it is intended for installations based in WP4.5 and BP2.5.2. I cannot assure it works for other versions.

    NOTE: As a BP plugin file will be modified, the trick will not survive BP updates.

    1) Make sure that users cannot create groups by default (Settings -> BuddyPress -> Options).

    2) Choose the users that you want to enable (to create groups). Get the list of WP capabilities for each of them from here (or use a role/capability editing plugin):

    https://codex.wordpress.org/Roles_and_Capabilities

    Choose one capability which is only shared by those users that will have the ability to create groups.

    For example, if you want WP roles Contributor, Author, Editor and Admin to have the ability to create groups but you want to exclude Subscribers, then choose “delete_posts” or “edit_posts” as your differentiating capability (they all -but Subscriber- can “delete_posts”).

    Combinations of custom Roles and Capabilities also work. In my case, I am using s2member Roles and Capabilities (i.e. “access_s2member_level2”).

    The choosen capability will be called my_choosen_capability beyond this point.

    3) Then, edit /path/to/your/installation/wp-content/plugins/buddypress/bp-groups/bp-groups-template.php. In line 4312 you will find the following function:

    function bp_user_can_create_groups()

    After the if structure that enables Admins to create groups regardless the settings, add this:


    if(current_user_can('my_choosen_capability')){
    return true;
    }

    In my case as my_choosen_capability = access_s2member_level2, the code looks like this:


    if(current_user_can('access_s2member_level2')){
    return true;
    }

    Then save and enjoy till next update.

    4) When the enabled users visit the group page, two buttons will be printed:

    -Join group
    -Create group

    This is it. Have fun!

Viewing 13 replies - 1 through 13 (of 13 total)

  • shanebp
    Moderator

    @shanebp

    It is always a bad approach to edit core files.
    That’s why a filter is provided to accomplished exactly the task described above.
    apply_filters( 'bp_user_can_create_groups', $can_create, $restricted );


    itsasiak
    Participant

    @itsasiak

    I tried the filter way you propose by adding a function and action to bp-custom.php but I could not make it work. That is why I wrote this little guide.

    Anyway, @shanebp is right, modifying core files is not the best practice. This is more like a last resource.


    jmprestidge
    Participant

    @jmprestidge

    @shanebp can you please provide accurate code for completing this task…if possible I would like to use user roles rather than capabilities to determine who can and can’t create groups.

    In my case I would like admins and a custom user type called management to be able to create groups


    David Cavins
    Keymaster

    @dcavins

    I generally stick to using capabilities. So I’d find a capability that all of the roles you want to be able to create groups have. Then the code looks like:

    add_filter( 'bp_user_can_create_groups', function( $can_create ) {
    	$can_create = false;
    	if ( current_user_can( 'known_capability' ) ) {
    		$can_create = true;
    	}
    	return $can_create;
    } );

    If you want to wrestle with the confusion of roles, this might work:

    add_filter( 'bp_user_can_create_groups', function( $can_create ) {
    	$can_create = false;
    	$user = wp_get_current_user();
    	if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'my_custom_role_name', (array) $user->roles ) ) {
    	    $can_create = true;
    	}
    	return $can_create;
    } );

    TheArmory1
    Participant

    @auctionarmory

    @dcavins,

    Where do we find the users capabilities?

    We are having a lot of issues with this. Is there a way to “review/ pending for approval” the groups before going live.


    David Cavins
    Keymaster

    @dcavins

    See above for a list of user capabilities by role:

    2) Choose the users that you want to enable (to create groups). Get the list of WP capabilities for each of them from here (or use a role/capability editing plugin):

    https://codex.wordpress.org/Roles_and_Capabilities

    Choose one capability which is only shared by those users that will have the ability to create groups.

    No, there is not currently a way to make a group “pending” though that’s will be possible as a result of something I’m working on.


    TheArmory1
    Participant

    @auctionarmory

    @dcavins,

    we allow vendors to post products so could we use something like this?

    add_filter( ‘bp_user_can_create_groups’, function( $can_create ) {
    $can_create = false;
    if ( current_user_can( ‘edit_products’ ) ) {
    $can_create = true;
    }
    return $can_create;
    } );


    David Cavins
    Keymaster

    @dcavins

    Hi @auctionarmory-

    That looks promising! The only way you can know if it “works” is by trying it in your environment. Create a couple of test users, one who’s got the somewhat elevated role of “vendor” and one who’s just a subscriber. Then try it when logged in as your test users. If your vendor can do create groups, but your subscriber can’t (and your admin still can) you’ve nailed it!

    Good luck!


    TheArmory1
    Participant

    @auctionarmory

    @dcavins, question should I enable group creation for all users and use this code or leave that option off.


    David Cavins
    Keymaster

    @dcavins

    If you uncheck “allow group creation for all users” only site admins will be able to make groups.


    TheArmory1
    Participant

    @auctionarmory

    @dcavins, No luck if I uncheck box only admins can create groups regardless of the code above.

    If I check that box ever role type has the ability to create a group regardless of the code above.


    David Cavins
    Keymaster

    @dcavins

    Actually your code will work either way (no matter what the restrict group creation checkbox is set to). Your function must not be “seen” by WP. Is it in bp-custom.php or in your theme’s functions.php file?


    TheArmory1
    Participant

    @auctionarmory

    @dcavins, Yes in the functions.php file.

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