How to distinguish between user roles in wordpress?

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:

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

Thanks

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

Related posts

Leave a Reply

3 comments

  1. 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

  2. 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.