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™

conditionals in_category can't get working

s2Member Plugin. A Membership plugin for WordPress®.

conditionals in_category can't get working

Postby micb11 » August 28th, 2010, 8:10 am

I have a blog with structure as follow

Meetings Blog (Partent Category)
- cobham
- walton on thames
- woking
- guildford
- weybridge

when the user signs up he can become part of only when of the child categories. I have written conditionals in the php functions as follows and it all works a treat

add_action('template_redirect',"my_custom_capabilities");

function my_custom_capabilities()
{
if(is_category("cobham")&& !current_user_can("access_s2member_ccap_cobham"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("dorking") && !current_user_can("access_s2member_ccap_dorking"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("guildford") && !current_user_can("access_s2member_ccap_guildford"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("richmond") && !current_user_can("access_s2member_ccap_richmond"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("tunbridge-wells") && !current_user_can("access_s2member_ccap_tunbridgewells"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("virginia-water") && !current_user_can("access_s2member_ccap_virginiawater"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("walton-on-thames") && !current_user_can("access_s2member_ccap_waltononthames"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("weybridge") && !current_user_can("access_s2member_ccap_weybridge"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("wimbledon") && !current_user_can("access_s2member_ccap_wimbledon"))
{
header("Location: /you-cannot-view-this-content");
exit();
}
else if(is_category("woking") && !current_user_can("access_s2member_ccap_woking"))
{
header("Location: /you-cannot-view-this-content");
exit();
}

}

However when the user clicks on the parent category he can see all the latest from all blogs (which is fine) but if he clicks on a post from a blog he should not be able to access, he can. So I thought that if I wrote some more conditional using in_category that should stop it and boot him out. But instead when I write an in_category conditional the user can no longer access any of the child categories.

Any help would be apreciated

Thanks
User avatar
micb11
Experienced User
Experienced User
 
Posts: 3
Joined: August 28, 2010

Re: conditionals in_category can't get working

Postby Jason Caldwell » August 30th, 2010, 9:37 am

Thanks for the great question.
I'm updating your rank to "Experienced User".
~ It's great to see developers integrating s2Member in more advanced ways.

OK. So let me clarify how these two functions work:

1. is_category()
This checks to see if the current WP query is an Archive view of a specific Category, and if so, is it the specific Category that you ask it about? i.e. is_category("green-grapes").

2. in_category()
This function is different. Instead of looking at the current WP query, it looks at the current Post, ( or the first Post in an archive view ). So using this function works great, but ONLY if you know that you're on a Singular Post or Page, and NOT in an Archive view of your Blog.

In other words, this is how you should use in_category()
Code: Select all
if(is_singular() && in_category("green-grapes))

Now, how to apply this to your current set of Conditionals ( as seen above ).
Here is a brief example of how you might accomplish what you need:

Code: Select all
if((is_category("cobham") || (is_singular() && in_category("cobham"))) && !current_user_can("access_s2member_ccap_cobham"))
{
header("Location: /you-cannot-view-this-content");
exit();
}

In other words, if the User is attempting to access the Archive for "cobham", OR (||) if they're on a Singular Post that is nested into the "cobham" Category.
~ 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: conditionals in_category can't get working

Postby micb11 » August 31st, 2010, 4:42 am

Hi Jason

Thanks for that , works a treat!!

Do you also know if there are any conditionals with buddypress. I would have liked to restrict viewing members profiles as well

for example

if member is in ccap_cobham and memberprofile is in ccap_cobham
then let them view page
else
redirect to no access page

Any Ideas
User avatar
micb11
Experienced User
Experienced User
 
Posts: 3
Joined: August 28, 2010

Re: conditionals in_category can't get working

Postby Jason Caldwell » August 31st, 2010, 6:57 pm

micb11 wrote:Do you also know if there are any conditionals with buddypress. I would have liked to restrict viewing members profiles as well

There are a couple of ways to accomplish this:

1. The Super Easy way:
Go to s2Member -> General Options -> URI Level Restrictions
enter the following word fragment:
Code: Select all
/members/

2. Advanced Conditionals integrated into your theme:
Code: Select all
if(preg_match("/\/members\//", $_SERVER["REQUEST_URI"])){
    header("Location: http://example.com/you-cannot-view-this-content");
    exit();
}

If you're running BuddyPress, be sure to use s2Member v3.2.4+
s2Member v3.2.3 had a bug related specifically to BuddyPress, which was corrected in 3.2.4+.
http://www.primothemes.com/post/s2membe ... th-paypal/

Also, be sure to use the full URL in the header redirection call.
Some browsers choke when you put in a relative URL.
So use the full URL, starting with http.

Code: Select all
header("Location: http://example.com/you-cannot-view-this-content");
~ 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


Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests

cron