PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Restricting URI access based on ccap in functions.php ?

s2Member Plugin. A Membership plugin for WordPress®.

Restricting URI access based on ccap in functions.php ?

Postby BoweFrankema » September 6th, 2010, 7:09 am

Hi Jason,

I'm really digging into S2Member now, and I want to use it for another project of mine. I've decided that Custom Capabilities are the best solution for my needs, and now I want to use it to protect specific BuddyPress Groups (where support and downloads can be found). So my idea was to do this:

Make a new Custom Capability for every new Premium Group which add that tag that I would like to use to define that membership.

Then go into my themes functions.php file and restrict the access to that group to the member with that custom capability added, like you showed in the video. But since I can't write any PHP I have no idea how to write the code that allows me to block someone from a hard coded URL. In the video example you use Image
for restricting access based on tags or categories.

So what I would like to do is create levels that allows access to a certain group URL only.
but I have no idea to modify it to replace tags/categories with an hardcoded url or a BuddyPress conditonal tag. Any ideas how to achieve this?

Thanks in advance,
Bowe
User avatar
BoweFrankema
Registered User
Registered User
 
Posts: 11
Joined: August 24, 2010

Re: Restricting URI access based on ccap in functions.php ?

Postby Jason Caldwell » September 7th, 2010, 10:14 pm

Hi Bowe. Thanks for the great question.

Here is how you would integrate BuddyPress Conditional Tags:
Code: Select all
<?php
add_action 
("template_redirect", "my_custom_capabilities", 1);
function my_custom_capabilities ()
    {
        if (bp_is_group_forum () && !current_user_can ("access_s2member_ccap_forums"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
    }
?>

So you're just swapping out a native WordPress® Conditional tag like has_tag(), and instead using a specialized BuddyPress Conditional Tag, like: bp_is_group_forum(). Other BuddyPress Conditional Tags are documented here: http://codex.buddypress.org/developer-d ... late-tags/

--------------------------------

Now, if instead; you wanted to bypass all Conditional Tags, and go straight to the URI itself, you would test word fragments against the $_SERVER["REQUEST_URI"]. So something like this:
Code: Select all
<?php
add_action 
("template_redirect", "my_custom_capabilities", 1);
function my_custom_capabilities ()
    {
        if (fnmatch ("/activity*", $_SERVER["REQUEST_URI"]) && !current_user_can ("access_s2member_ccap_activity"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
        else if (fnmatch ("/forums*", $_SERVER["REQUEST_URI"]) && !current_user_can ("access_s2member_ccap_forums"))
            {
                header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
                exit ();
            }
    }
?>

So here we are testing word fragments against $_SERVER["REQUEST_URI"].

Important to note:
- A URL looks like this: http://domain/path/to/file.php
- A URI is just the path: /path/to/file.php ( and this is what $_SERVER["REQUEST_URI"] represents ).

The function fnmatch() is documented here, and it's very simple.
The asterisk (*) represents a wildcard.
http://php.net/manual/en/function.fnmatch.php
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Restricting URI access based on ccap in functions.php ?

Postby BoweFrankema » September 9th, 2010, 12:14 pm

Jason thank you thank you thank you :D

This work perfectly! I'm working on a Premium BuddyPress Theme with S2Member integration and some blog posts on how to achieve BuddyPress specific things with it.. Seriously this plugin is so powerful it blows my mind.

When the theme is released I'll send you a copy so you can take a look :)

Thanks again!
User avatar
BoweFrankema
Registered User
Registered User
 
Posts: 11
Joined: August 24, 2010

Re: Restricting URI access based on ccap in functions.php ?

Postby Jason Caldwell » September 9th, 2010, 10:59 pm

You are VERY welcome Bowe.
Thanks for the kudos!
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Restricting URI access based on ccap in functions.php ?

Postby startasocialnetwork » November 26th, 2010, 9:01 pm

Hey Jason,

I know since this post (and the video) you've added custom capabilities fields to pages/posts to make using ccaps easier. Is the above stratgey still the only way to work with ccaps and URIs, or has this also been improved?

Thanks
Ric
User avatar
startasocialnetwork
Registered User
Registered User
 
Posts: 23
Joined: October 8, 2010
Location: Vancouver, BC

Re: Restricting URI access based on ccap in functions.php ?

Postby startasocialnetwork » November 27th, 2010, 12:48 pm

Also, can these two strategies be combined?

That is, Restrict access by URI, but also use the buddypress conditionals for fine-tuning (like only letting some people create new groups)?
User avatar
startasocialnetwork
Registered User
Registered User
 
Posts: 23
Joined: October 8, 2010
Location: Vancouver, BC

Re: Restricting URI access based on ccap in functions.php ?

Postby svenl77 » December 1st, 2010, 8:18 am

Hi, I needed to add current_user_can('level_10'), otherwise the admin has no access to custom capability groups.

function my_custom_capabilities ()
{
if(current_user_can('level_10'))
{
return false;
}
...
User avatar
svenl77
Registered User
Registered User
 
Posts: 4
Joined: December 1, 2010

Re: Restricting URI access based on ccap in functions.php ?

Postby peeld » August 25th, 2011, 2:31 am

Just digging through S2 and it looks like the above URI example is the way I'm going to need to go; I need to restrict certain BuddyPress groups so users have access only to the group they've paid for (a classroom scenario). When I put that code in my theme's function.php file (theme = custom community pro), it either breaks it or doesn't work :(

Any new suggestions on what I could be doing wrong? I'm happy to submit my functions.php file...

Daisy
User avatar
peeld
Registered User
Registered User
 
Posts: 97
Joined: August 24, 2011

Re: Restricting URI access based on ccap in functions.php ?

Postby peeld » August 25th, 2011, 5:20 pm

Just an update, I've got it all working GREAT, thanks to forum posters for their solutions.

Here's the solution: viewtopic.php?f=36&t=1405

EDIT 11/6/2011: As of BP 1.5 for some reason my code doesn't work anymore, FYI. Trying to troubleshoot.
User avatar
peeld
Registered User
Registered User
 
Posts: 97
Joined: August 24, 2011


Return to s2Member Plugin

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 2 guests

cron