How to Show or Hide Content in WordPress / WPMU Based on User Authentication, Roles, and Capabilities

Here is a handy little code snippet that falls into the basic category for WordPress but can prove to be extremely useful when customizing your installation. Whether you’re working with WordPress, WPMU or BuddyPress, these little tips will give you greater flexibility in building your community.

1
2
3
4
5
6
7

<?phpif(is_user_logged_in()){//only logged in user can see this ?><div>
I love green tea!
</div><?php}?>

You can also selectively show content using role-based permissions, as detailed in the WordPress codex section on roles and capabilities. For example, if you want to show a specific menu to your admins and editors only but show a different menu for everyone else, you can do something like this:

1
2
3
4
5
6
7
8
9
10
11

<?phpif( current_user_can('delete_others_posts')){//only admins and editors can see this ?><ul><li>Secret Menu Item</li><li>Other Restricted Link</li></ul><?php}else{?><ul><li>General Menu</li><li>Anyone Can See This</li></ul><?php}?>

These is a starting point so that you can write your own.There are some plugins which accomplish these things. However, sometimes you cannot find a plugin specific enough to do what you need and you only want it applied in one area. That’s when these quick snippets will come in handy.

Other possible uses might include:

Hide signup information from logged-in users and re-appropriate that space on the page targeted to your subscribers.

If you’re using BuddyPress, you could tweak it to selectively target profile areas to be private unless the user is logged in.

If you use your imagination, I’m sure that you can find unlimited ways to show and hide content. In this age of social networking, users’ attention spans are decreasing and they will often click away from sites that instantly offer too much cluttered information. One of the most important reasons to show and hide information based on a user’s role is to conserve the real estate on the page and strategically target the information on your site to registered or non-registered users. These simple snippets are a good foundation for how to use BuddyPress conditional tags, for which we will provide an introduction next week. Stay tuned!

Sarah Gooding is a partner at Untame, a boutique digital marketing firm specializing in open source content management systems and social networking architecture. She enjoys staying on top of the latest WordPress and BuddyPress news. Get in touch with Sarah on Twitter @pollyplummer

Or how about trying out our plugins and themes for free?

Test out your WordPress Superpowers and give your site a boost with all our plugins, themes, services and our full support, free for 14 days - no catch!

10 Responses

Thanks for this post; this is a useful function I didn’t know about. I work for a digital media company and we specialize in user interface web design. we are always looking for new ways to get genuine feedback from users to improve usability. This would be a great supplement to our blog, where users can post content. Our blog archive has a lot of information about other usability tips for web design applications as well.

This is a good article. I use buddypress and WPMU. I want to not allow the authors on my website see a number of things such as if you go to the edit post page – I don’t want them to see the drafts of other users. Is it possible to do so. Can you please provide some guidance for this.

Can you give a name of some plugins that have such functionality? I was looking for such, but couldn’t find.
What I need, more specifically, is a plugin that hides links in the main menu of private pages to the unregistered users. If you are registered – you should see the links. Be default if you are not registered – you see the links to the private posts, but when you click them – it gives 404, which is stupid.