Tutorial: Content For Admin Only
For a couple of days now, I’ve being wondering how to display something only for the admin to see, when the admin is logged in. This could come in handy to display links to the WP admin panel or stats page.
So I did a bit of research and I found exactly what I was looking for:
<?php global $user_ID; if( $user_ID ) : ?>
<?php if( current_user_can('level_10') ) : ?>
<a href="http://yourdomainurl.com/stats/">Stats</a>
<?php else : ?>
<?php endif; ?>
<?php endif; ?>
The code above will display “Stats” with a link to a stats page, but only for the admin, when the admin is logged on.
You can modify the code above to display links only for certain types of users, such as Contributors, Authors, Editors, etc, by changing the “level_10“. Read more about WordPress Roles and Capabilities here.
This is just a quick little tutorial I wanted to share with you guys! Enjoy. Oh and if you know of any other ways to use this tutorial, please share!
26 Responses to “Tutorial: Content For Admin Only”
Trackbacks
- Contenido solo para administradores en WordPress > Miniposts > RUDEWORKS
- WordPress Role Manager ‹ The Blog of Chris Thomson
- WPCandy » WP Easy Admin Plugin
- Display User Welcome Message - Wordpress Hut
- How To: Displaying Content Only For The Admin To See
- 25+ Essential Articles For Every WordPress Beginner






Alain
Posted on January 23rd, 2008 at 11:51 pm
Will come in handy on my next blog! Thanks!
Thássius V.
Posted on January 23rd, 2008 at 11:53 pm
Nice tip. Does it work, for example, if I want to show a centain content starting on level 5 users up to level 8 users?
// http://memoriasfracas.com
Sumesh
Posted on January 24th, 2008 at 3:06 am
I’ve used it to display a navigation-bar link to the WP admin, one to GAnalytics stats page, one to my Google reader etc.
My site itself is a portal for me now - all links for my daily reading is there.
anima
Posted on January 24th, 2008 at 3:26 am
exactly what I need for now
Thanks!
Ali Salem
Posted on January 24th, 2008 at 4:12 am
Definitely a nice trick.
Tao
Posted on January 24th, 2008 at 6:08 am
That is a really neat trick!
I can think of lots of little hacks to implement using that!
Mike Smith
Posted on January 25th, 2008 at 2:11 pm
Thanks for the great posts here. I’m a first time reader, but this site is instantly in my google reader! You could also use this method to have a “welcome” message at the top of the content welcoming back the “writer” or maybe hacking into the code a bit more to display the viewers username. Adding a bit of ‘extra’ to the site to help keep your writers happy.
I looked through the wordpress codex and found these two code snippets. thought they might help.
and another one was:
user_login;
} else {
$username = “Guest”;
} ?>
I haven’t tested these codes yet, but they sound just about right
Michael Castilla
Posted on January 25th, 2008 at 3:25 pm
@Mike: Definitely man. That’s a great idea. I’ll look more into that!
And thanks for putting us in your reader
Enjoy!
Ryan
Posted on January 26th, 2008 at 7:11 am
Thanks.
That will definitely come in handy. I’d be wondering how to do this for a while, but never got round to figuring it out.
Mike Smith
Posted on January 27th, 2008 at 4:40 am
Hey. I just wanted to let you know I wrote up a tutorial explaining what I was talking about a little bit more, along with some code samples. I’ve linked the article here as well so people reading it know where I originally got the idea from.
The site’s not 100% done yet as you can see, but I wanted to start getting content going up over the next couple days while i finish everything off.
http://wordpresshut.com/display-user-welcome-message/
Thanks,
Mike
SE7EN
Posted on January 27th, 2008 at 7:36 am
Thank you!
Now I can add stat code that doesn’t count my own visits, yay!
Spiewgels
Posted on January 29th, 2008 at 3:43 pm
This got me to thinking…I wanted to know if there was a snippet of php code I could add in order to make it so that any user who wasn’t level 10 (in other words, not me), could not edit my posts/pages even if they could modify everyone else’s. Pretty much I want the editor to be able to do their job without touching anything of mine. To cope with it thus far, I’ve made so that they can’t edit published pages/posts, while they can modify something that is pending review. This, obviously, is inefficient.
If there is such a code, where would I put it in the original files, or could I just add it as a plugin?
SE7EN
Posted on January 30th, 2008 at 3:27 pm
uhhhm, how can I insert content for “non-admin” only
I put the content after
?php else : ?
instead
but nothing shows, as admin and non-admin
I don’t know what’s wrong
Michael Castilla
Posted on January 31st, 2008 at 6:18 pm
@Se7en: I don’t understand your question. Please clarify!
SE7EN
Posted on February 2nd, 2008 at 3:58 am
Sorry for my poor english
How to add content for non-admin?
I want to add stat tracking code that only works when visitors visit my pages, not me
so it won’t track my own visit
Ryan
Posted on March 14th, 2008 at 9:01 am
Thanks, this will prove invaluable for a new plugin I am developing. I’ll add a link in my credits for you.
Noura
Posted on April 2nd, 2008 at 6:50 pm
Hi Michael,
Thanks for this nice tutorial.
I was wondering if you could help me with this:
I want to make the users who have Author role to just view or edit their own posts. I know how to make them edit their posts, but the problem is that i don’t want them to view others post in the admin panel, also i don’t want to give them control with any comments.
Your help is greatly appreciated,
Noura
Neil Stead
Posted on May 12th, 2008 at 1:36 pm
The use of User levels has been deprecated since Wordpress V2.0 - it’s there for backwards compatibility, but there’s a better way to do it…
Have a look at the Role Manager plugin here:
http://www.im-web-gefunden.de/wordpress-plugins/role-manager/
This allows you to create new “capabilities”, and assign them to User roles (you can also create new User roles).
So, for your example, you could create a capability called “view-stats”, and assign it to the “Administrator” role.
Then in your code, use the following:
<?php if (current_user_can(’view-stats’) ) : >
etc.
Neil.
Marci :)
Posted on July 1st, 2008 at 12:46 pm
Many of you who have commented here will be interested in this new plugin called Role Scoper (http://agapetry.net/news/introducing-role-scoper/). It’s an absolute God-send!! It adds to the functionality of Role Manager in ways that replace about a dozen other plugins. It’s in public beta, with little in the way of instructions, but if you’ve ever struggled with wanting to customize your users roles and capabilities at a much deeper level, you should really look into this one! (I did NOT create it, I just found it and am using it.)
Josh
Posted on August 21st, 2008 at 4:08 pm
Put an exclamation mark before
current_user_can. So, it becomes!current_user_can.