tags:

views:

719

answers:

3

I am trying to limit what content the subscribers can see but nothing I do seems to work. I have spent hours trawling the web and through the wordpress code, all to no avail. Does anyone know how I would be able to go about this?

Ideally the code structure would look like:

if(get_role() = 'subscriber'){
    redirect
}

Thanks

Incidentally i have tried get_role($role) and that doesn't work for me.

A: 

update: there's a function current_user_can(capability) which you can use to find out what user can and cannot do. i'd imagine that you will need to add another role or capability.

try the following:

if ('subscriber' == get_role()) {
    # do whatever
}

= assignment
== comparison

SilentGhost
thanks man. i always forget that. alas it's not quite the fix.
Drew
A: 

Wordpress doesn't do content access limitations very well. It's just not built into the system. Most of the permissions work involves controlling who can create and edit, but the overriding design principle is to let anyone view. This makes sense for a blog app, but it can be limiting if you have user types that need conditional access.

You can add a subscriber role without much trouble, but then you'll have to add hooks to lots of the rendering code to either redirect or hide posts from the loop. The least amount of dev work is to check the user's role in the loop and hide what they shouldn't see. The drawback to this is you may end up rendering near blank pages.

Todd Rowan
+1  A: 

I've used current_user_can for this. There's a list of roles and capabilities here:

http://codex.wordpress.org/Roles_and_Capabilities#Capabilities:_5

So, since everyone above the level of "subscriber" can edit posts, one way to accommodate the requirement you've outlined would be:

if (!current_user_can('edit_posts')){

//redirect, error, etc as you like

}

HTH, gene

PDXGene
Nice work, cheers dude!
Drew