How to sell custom Capabilities to BuddyPress Group?

s2Member is amazing, so is BuddyPress.

I want to sell Custom Capability access to BuddyPress groups but I’m stumped on how to do it.

The request_uri is easy to identify but the s2Member Restriction Options only provide for restricting my target URL by member level, which in this case is not helpful.

I have many members, they are all at the same Level #1 (Basic Member.) Then I restrict various areas of the BuddyPress site by Custom Capabilities.

I would like to make Paid Groups available but I can’t figure out how to restrict a BuddyPress Group URI by custom capability.

Would you be so kind as to offer guidance on how I can accomplish this goal?

All the best

Richard

Try adding this in an mu-plugin:

<?php
// Only allows access to a specific URI if user has the corresponding Custom Capability
function s2_restrict_uri_by_ccap() {
	$allowed = TRUE;
	
	if ( fnmatch ( '/members/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_members' ) ) {
		$allowed = FALSE;
	}
	
	// If any of the URIs above matched, but the user did not have the necessary ccap, redirect to Membership Options
	if ( FALSE === $allowed ) {
		header ( 'Location: ' . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL );
		exit;
	}
}
add_action ( 'wp', 's2_restrict_uri_by_ccap', 1 );

It protects a URI of /members/ so that only those with a ccap of members can access it.

2 Likes

Tim

Thank you,

This code worked Perfectly!

(For those reading this needing to know how to add a mu-plugin, here is a simple guide https://gregrickaby.com/2013/10/create-mu-plugin-for-wordpress/

s2Member REALLY needs a GUI friendly way to do this same thing in the Restriction Options.

How can we get this type of function added to the s2Members dashboard controls?

Richard

My pleasure!

Add your voice here: https://github.com/websharks/s2member/issues/336

Hi Tim,
I’m trying to do this. I have a custom capability that I want to restrict access to certain areas in the BuddyPress, like: groups, achievements, forum. Is something like this correct?

if ( fnmatch ( ‘/groups/’,’/achievements’,‘forum’, $_SERVER[‘REQUEST_URI’] ) && !current_user_can( ‘access_s2member_ccap_fast_track’ ) ) {
$allowed = FALSE;
}

Susan, no, that won’t work. There are a few ways in which you could combine the conditions, but I think I’d favor doing it like this:

function s2_restrict_uri_by_ccap() {
	$allowed = TRUE;
	
	if ( fnmatch ( '/groups/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fast_track' ) ) {
		$allowed = FALSE;
	}
	else if ( fnmatch ( '/achievements/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fast_track' ) ) {
		$allowed = FALSE;
	}
	else if ( fnmatch ( '/forum/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fast_track' ) ) {
		$allowed = FALSE;
	}
	
	// If any of the URIs above matched, but the user did not have the necessary ccap, redirect to Membership Options
	if ( FALSE === $allowed ) {
		header ( 'Location: ' . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL );
		exit;
	}
}
add_action ( 'wp', 's2_restrict_uri_by_ccap', 1 );

tried it but didn’t work. I can restrict the members but not the other pages in BuddyPress. I will keep trying other combinations. I tried adding Members to the path but that didn’t work either. Thanks for your help.

It won’t. That’s not where the problem lies.

First, check that groups, achievements, and forum (a) all exist as part of the URL and (b) have a slash before and after them on the pages you are looking to protect. If one of those conditions is not true, this won’t work.

Second, if those conditions are true, try removing the two instances of else in the code I gave you.

Yes they exist as part of the URL and have slash before and after. I removed else and it still doesn’t work. This is the code I have now. I changed my custom capability to fasttrack.

<?php // Only allows access to a specific URI if user has the corresponding Custom Capability function s2_restrict_uri_by_ccap() { $allowed = TRUE; if ( fnmatch ( '/groups/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fasttrack' ) ) { $allowed = FALSE; } if ( fnmatch ( '/achievements/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fasttrack' ) ) { $allowed = FALSE; } if ( fnmatch ( '/forum/', $_SERVER['REQUEST_URI'] ) && !current_user_can( 'access_s2member_ccap_fasttrack' ) ) { $allowed = FALSE; } // If any of the URIs above matched, but the user did not have the necessary ccap, redirect to Membership Options if ( FALSE === $allowed ) { header ( 'Location: ' . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL ); exit; } } add_action ( 'wp', 's2_restrict_uri_by_ccap', 1 );

OK, try changing this:

header ( 'Location: ' . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL );

to this:

wp_redirect( S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL );

If that doesn’t work, I think either you are testing using the wrong user or else there is something else happening on your site that is preventing this from working.

Yes. Still not working. I will backtrack and see if I can find the problem. Thank you for your help.