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
<?php if (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
<?php if ( 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.
  • Sell and display ads targeted to your casual visitors or to your registered users.
  • Show or hide widgetized areas in your sidebar based on if the user is logged in or not.
  • Display alternate footers, sidebars, headers, page elements, etc. instead of having to create a custom template for each instance.
  • Reserve certain content for registered users only, ie “Members only downloads” or restricted galleries.
  • Attach it to your “Read More” links to get users hooked before requiring registration.
  • Create a separate menu bar for your clients for payment forms for registered users, etc.
  • 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!

Comments (10)

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

  2. Thanks of this. I’m still learning WordPress, but I’ve learned that I prefer to write my own plugins. Usually less code because it can be use specific. Just more to maintain later.

    This code will help create user types, based on roles. Trying to figure how to display different names for users (e.g. student, teacher, parent) based on role definitions.

    Any ideas?

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

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

Participate