Tutorial: Content For Admin Only

31 Comments

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!

31 thoughts on “Tutorial: Content For Admin Only

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

  2. Pingback: Contenido solo para administradores en WordPress > Miniposts > RUDEWORKS

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

  4. Pingback: WordPress Role Manager ‹ The Blog of Chris Thomson

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

  6. Pingback: WPCandy » WP Easy Admin Plugin

  7. Pingback: Display User Welcome Message - Wordpress Hut

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

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

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

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

  12. Pingback: How To: Displaying Content Only For The Admin To See

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

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

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

  16. Pingback: 25+ Essential Articles For Every WordPress Beginner

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

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

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

Comments are closed.