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!
Post Revisions:
- 16 August, 2010 @ 10:33 [Current Revision] by michael castilla
- 5 August, 2010 @ 4:51 by Ryan Imel
- 23 January, 2008 @ 23:49 by Ryan Imel
Posted January 23, 2008
Alain said:
Will come in handy on my next blog! Thanks!
on January 23, 2008 at 11:51 pm
Thássius V. said:
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
on January 23, 2008 at 11:53 pm
Sumesh said:
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.
on January 24, 2008 at 3:06 am
anima said:
exactly what I need for now
Thanks!
on January 24, 2008 at 3:26 am
Ali Salem said:
Definitely a nice trick.
on January 24, 2008 at 4:12 am
Tao said:
That is a really neat trick!
I can think of lots of little hacks to implement using that!
on January 24, 2008 at 6:08 am
Mike Smith said:
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
on January 25, 2008 at 2:11 pm
Michael Castilla said:
@Mike: Definitely man. That’s a great idea. I’ll look more into that!
And thanks for putting us in your reader
Enjoy!
on January 25, 2008 at 3:25 pm
Ryan said:
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.
on January 26, 2008 at 7:11 am
Mike Smith said:
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
on January 27, 2008 at 4:40 am
SE7EN said:
Thank you!
Now I can add stat code that doesn’t count my own visits, yay!
on January 27, 2008 at 7:36 am
Spiewgels said:
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?
on January 29, 2008 at 3:43 pm
SE7EN said:
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
on January 30, 2008 at 3:27 pm
Michael Castilla said:
@Se7en: I don’t understand your question. Please clarify!
on January 31, 2008 at 6:18 pm
SE7EN said:
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
on February 2, 2008 at 3:58 am
Ryan said:
Thanks, this will prove invaluable for a new plugin I am developing. I’ll add a link in my credits for you.
on March 14, 2008 at 9:01 am
Noura said:
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
on April 2, 2008 at 6:50 pm
Neil Stead said:
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.
on May 12, 2008 at 1:36 pm
Marci :) said:
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.)
on July 1, 2008 at 12:46 pm
Josh said:
Put an exclamation mark before
current_user_can. So, it becomes!current_user_can.on August 21, 2008 at 4:08 pm
Vincent said:
It would be nicer if you installed a plugin like “code markup” to display source code more elegantly. Anyway, thanks for the info. I just happen to need this for a plugin.
on October 9, 2008 at 10:02 am
WP Tricks said:
This is what I looking for
on March 3, 2010 at 6:06 am