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:

Tagged with:

About the author:

31 Responses to Tutorial: Content For Admin Only

  1. Alain says:

    Will come in handy on my next blog! Thanks!

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

  3. Sumesh says:

    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.

  4. anima says:

    exactly what I need for now :D
    Thanks!

  5. Ali Salem says:

    Definitely a nice trick. :)

  6. Tao says:

    That is a really neat trick!

    I can think of lots of little hacks to implement using that!

  7. [...] 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 [...]

  8. 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 :)

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

  10. @Mike: Definitely man. That’s a great idea. I’ll look more into that!

    And thanks for putting us in your reader :) Enjoy!

  11. Ryan says:

    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.

  12. [...] 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 [...]

  13. [...] 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 [...]

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

  15. SE7EN says:

    Thank you!
    Now I can add stat code that doesn’t count my own visits, yay!

  16. Spiewgels says:

    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?

  17. SE7EN says:

    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

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

  19. SE7EN says:

    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

  20. [...] 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 [...]

  21. Ryan says:

    Thanks, this will prove invaluable for a new plugin I am developing. I’ll add a link in my credits for you.

  22. Noura says:

    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

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

  24. Marci :) says:

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

  25. Vincent says:

    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.

  26. WP Tricks says:

    This is what I looking for

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

  28. ontavu says:

    Thank you very much.
    I was looking for this!

  29. Leeroy says:

    Hey fellas,

    I have a php file with a form that appends to a CSV file on the server. It’s at http://example.com/stuffs/my-db-entry-form.php and in that same folder is the mydb.csv file.

    I want to restrict access to this PHP file and make it accessible only to users who are logged into WordPress (as those guys will be doing the actual work filling in the form with new database entries).

    What do I have to do to put my-db-entry-form.php “inside” WordPress and restrict access to it?

Leave a Reply

Please note that WPCandy is a moderated community.

 

Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>