• 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!

    Post Revisions:

    Posted January 23, 2008

22 comments

  1. Alain said:

    Will come in handy on my next blog! Thanks!

    on January 23, 2008 at 11:51 pm Reply

  2. 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 Reply

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

  4. anima said:

    exactly what I need for now :D
    Thanks!

    on January 24, 2008 at 3:26 am Reply

  5. Ali Salem said:

    Definitely a nice trick. :)

    on January 24, 2008 at 4:12 am Reply

  6. 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 Reply

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

  8. 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 Reply

  9. 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 Reply

  10. 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 Reply

  11. 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 Reply

  12. 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 Reply

  13. 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 Reply

  14. Michael Castilla said:

    @Se7en: I don’t understand your question. Please clarify!

    on January 31, 2008 at 6:18 pm Reply

  15. 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 Reply

  16. 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 Reply

  17. 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 Reply

  18. 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 Reply

  19. 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 Reply

  20. 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 Reply

  21. WP Tricks said:

    This is what I looking for

    on March 3, 2010 at 6:06 am Reply


RSS feed for comments on this post.

Leave a comment

Trackbacks on this post

  1. Contenido solo para administradores en WordPress > Miniposts > RUDEWORKS

    [...] Artículo (RSS) Desde la genial WPCandy, sencillísimo tutorial para incluir contenido sólo para administradores logeados en nuestros themes. Muy útil para webs de clientes que no quieren complicarse la [...]

  2. WordPress Role Manager ‹ The Blog of Chris Thomson

    [...] other day I ran across a blog post at WPCandy called “Tutorial: Content for Admin Only” and that reminded me of this WordPress [...]

  3. WPCandy » WP Easy Admin Plugin

    [...] couple of days ago I wrote a tutorial on how to display content for only the admin to see. I kept thinking a bit more about what else I could do with this WordPress trick and I ended [...]

  4. Display User Welcome Message - Wordpress Hut

    [...] I was reading a post in my google reader from over at WP Candy and it caught my eye. It was a tutorial on how to display [...]

  5. How To: Displaying Content Only For The Admin To See

    [...] lot of use out of it. Much along those same lines, Michael of WPCandy has written a post explaining how to display content so only the admin can view it. This goes well beyond the edit button and allows you to setup just about anything to display for [...]

  6. 25+ Essential Articles For Every WordPress Beginner

    [...] Tutorial : Content For Admin Only . . [...]

  7. Os Melhores Artigos Fevereiro 25 through Março 3 | Sérgio Designtrends

    [...] Tutorial: Content For Admin Only — WPCandy — WordPress Themes, Plugins, Tips, and Tricks – [...]

TrackBack URL