How To Show Content For Specific Users In WordPress Templates

by Dale Mugford ~ May 02 / 2008

One thing about building WordPress templates I haven’t seen a lot of documentation on is user-specific content. There are a few plugins out there that help with adjusting and assigning roles and capabilities, and there’s a good wiki document on the WordPress Codex site for defining the roles and capabilities that exist in WordPress, but not a lot about how to craft up some template codes to produce content for specific users.

Let’s say you have a little box on your sidebar you use to greet your subscribers (registered users), and also want to use it as a control box for your editors, authors, and contributors, while also as the administrator, have your own custom content there.

The first thing we’ll do is find out what levels our roles are at. On a default WordPress install of 2.5, the roles and the max. role number hierarchy are as follows:

Administrator: level 10
Editor: Level 7
Author: Level 4
Contributor: Level 2
Subscriber: Level 0
*all role levels also contain the capabilities of those below them

Now, there’s a little bit of a mess with regard to all the various capabilities assigned to each role, so if you want the long play version of what this is all about, visit the codex for the nitty gritty.

So now, we know the number assigned to each role, but how do we identify who’s using the site and serve up dynamic content for them? WordPress knows who you are when you’re logged in, so we just have to ask it to show different content when it identifies users’ roles.

It’s a good thing we can do this easily too, and we can add some DIVs around it, inside it, and style up our box any way we see fit.

To start, let’s create dynamic content showing for an administrator and subscriber, and something that will show by default to those not logged in:

<?php
if (current_user_can('level_10')) : ?>
<?php print "control the world"; ?>
<?php elseif (current_user_can('level_0')) : ?>
<?php print "read and comment only"; ?>
<?php else : ?>
<?php print "you gotta log-in to see the goodies"; ?>
<?php endif; ?>

With this code if an admin is logged in and viewing the page, “control the world” will be shown. If a subscriber is logged in, “read and comment only” will be shown to them instead. For users not logged in or registered, “you gotta log-in to see the goodies” will be shown.

Let’s go further, and add the other roles:

<?php
if (current_user_can('level_10')) : ?>
<?php print "control the world"; ?>
<?php elseif (current_user_can('level_7')) : ?>
<?php print "edit, write, publish, delete, read, comment"; ?>
<?php elseif (current_user_can('level_4')) : ?>
<?php print "write, publish, delete, read, comment"; ?>
<?php elseif (current_user_can('level_2')) : ?>
<?php print "write, read, comment"; ?>
<?php elseif (current_user_can('level_0')) : ?>
<?php print "read and comment"; ?>
<?php else : ?>
<?php print "you gotta log-in to see the goodies"; ?>
<?php endif; ?>

Of course you can go to town here, and have entire loops, pages, and posts setup to be displayed dynamically to different roles when users are logged in. This method provides an excellent way to serve premium content on your WordPress site, dynamically. Of course a plugin like WP-Cache won’t work too well in this case because the dynamic content would get eaten up by the cache system.

There is a workaround there, though. With WP-Cache, you can force the cache to go and get another php file and include it outside of the cache:

<!--mclude users.php-->
<?php include_once(ABSPATH . '' . get_bloginfo('template_directory') . '/users.php'); ?>
<!--/mclude-->

In the above case, we put our dynamically-served role content into a separate php file called ‘users.php’, and dropped that in our active themes’ directory. Then we put this code where we originally had it showing up before we activated WP-Cache. Abacadabra Batman: it’ll now stay dynamic.

Also, if you just want to show different content based on whether a user is logged in or not, you can use this template tag anywhere you like:

<?php if (is_user_logged_in()){ ... } ?>

An example where different content is displayed:

<?php if (is_user_logged_in()){
echo "Welcome, registered user!";
}
else {
echo "Welcome, visitor!";
};
?>

17 Comments

  1. 2 years, 4 months

    Wouldn’t a switch function be a little more elegant than elseif? Or perhaps an array would be even more flexible:
    http://ca.php.net/manual/en/control-structures.switch.php#70652

  2. Dale Mugford

    BNC Design Guru

    2 years, 4 months

    Sure, I realize it’s PHP hyperbole- but I’m considering mild to moderate WordPress users who can digest this, and make something of it. Also, the example is meant to be extrapolated such that you could have chunks of code for specific users, and the if statements would let users do that easily.

    Additionally, WordPress typically documents in this fashion. An example of the PHP hyperbole can be found in the default WordPress template for archives.

    P.S-> Our spam monster ate your first comment. I forced it to wretch and give it back : )

  3. Duane Storey

    BNC Development Guru

    2 years, 4 months

    There are obviously many different ways you can write the same piece of code. The speed of an if else pair is relatively insignificant well taken into the context of the tens of database calls that happen within WordPress on some pages. For most examples here, we’ll favor readability over other criteria such as speed, unless of course the code fragment demands fast execution or is involved in a big tight loop.

  4. 2 years, 4 months

    @DUANE: “…most examples here, we?ll favor readability over other criteria…”

    understood, thanks for the clarification!

  5. Dgold

    1 year, 9 months

    Thank you So Much for this helpful post. I learned it, and was able to implement this on my sidebar tonite. I use 2 other plugins, that help in association with this: Login Redirect sends you back to the page you were on when you clicked Login. TDO Mini Forms lets users submit posts from the Frontend. Now users will never need to see the wp-Admin and the Dashboard. Using your code, I put it so the “Site Admin” login (from the wp_register function) only appears after you are logged in as Admin.

  6. After searching Google for quite a while, this is the best article I’ve found on how to take advantage of the roles in templates. Thanks.

  7. greendude

    1 year, 5 months

    Awesome stuff! thank you.

  8. Dale Mugford

    BNC Design Guru

    1 year, 4 months

    You’re welcome- I’ll likely write another one, a little more advanced that ties into the types of content you can serve up based on roles, such as author interactivity on the front end.

  9. Jack

    1 year, 2 months

    thanks for useful post, but what can we use for show if admin is logged in or not, for example if (is_admin_logged_in()), anything like this ?
    thanks

  10. Dale Mugford

    BNC Design Guru

    1 year, 2 months

    @Jack: Well the if (current_user_can('level_10')) takes care of that.

  11. Jack

    1 year, 2 months

    i don’t wanna use the code for current user i wanna echo “Online” for example, if admin is logged in wordpress. thanks

    • Dale Mugford

      BNC Design Guru

      1 year, 2 months

      I take it you mean you want other users to know when an admin is around, correct? If so, you’d need javascript and Ajax to accomplish that.

  12. Jack

    1 year, 2 months

    i don’t wanna make it hard to just echo “online” or “offline” just if there is a function to use like is_user_logged_in, it will be better

  13. Dale Mugford

    BNC Design Guru

    1 year, 2 months

    Well ‘logged in’ could still mean they’ve left to the site, depending on whether they’ve told wordpress to ‘remember me’ or not. That checkbox determines whether someone is logged in only with a browser session cookie, or the cookie lasts beyond the browser session and they can come back and still be logged in.

    What I’m trying to determine is if you’re looking for whether they’re ‘online’ as in at the site at that moment or not.

  14. Jack

    1 year, 2 months

    actually not by using ajax and js, just when the user entered to the site see a line that said the admin is off or not, i used also wp-useronline and made an echo to return when admin with his special user is online, so show “online” and if not “offline” but a problem that there is, the code returns only “online” coz the useronline knows the ip and cookie, like whenever u comment on a site, when u wanna again comment u see your name and email is in their fields without filling them again. so it’s not a good way, i’m just trying to find sth to simply show it. thanks

  15. 5 months, 2 weeks

    Hi Dale;

    I am very new to all this wordpress stuff, and am just feeling may way around and learning as I go, my question is in which of the file does all this code reside?

    thanks

    • Dale Mugford

      BNC Design Guru

      5 months, 2 weeks

      Whatever template files you want to use them in : )