WordPress.org

Support

Support » Plugins and Hacks » Links for Admin only?

Links for Admin only?

  • Hi Folks,

    How can I display links on my sidebar, only if the admin user is logged in? I have a few links to other website/email resources that i’d like to have accessible from my WP 1.5 blog.

    Thanks,

    Dave

Viewing 11 replies - 1 through 11 (of 11 total)
  • This bit of code should do it:

    <?php
    global $user_login;
    get_currentuserinfo();
    if('admin' == $user_login) :
    ?>
    ~ display this only if admin is logged in ~
    <?php endif; ?>

    Following on from this, I’m wondering if it’s possible to display certain links if non-admin as well as admin users are logged in?

    For example, say you have a site where you yourself are the admin, and you have another user who has very low-level access, like read-only.

    I’m guessing you can change the variable which in the code above says ‘admin’ == $user_login, but I don’t know what you’d change ‘admin’ to. If your lowlever user’s username is “cabbage”, say, does that mean you’d put in ‘cabbage’ instead of ‘admin’?

    But would that mean that the admin user can NOT see the extra links? I guess what you’d want to do is have two variables in there, admin AND cabbage, but I don’t know how to write this in PHP …

    Please forgive my dumbness here, but if anyone could help I’d be most grateful!

    Regards,
    Croila

    croila, for that you can change the if line above to:

    if('admin' == $user_login || 'cabbage' == $user_login) :

    For multiple users just separate each test on $user_login with || (which means OR here).

    Also, if one needed to test on multiple logins for different sets of links, you can set up an if/else if statement like so:

    <?php
    global $user_login;
    get_currentuserinfo();
    if('admin' == $user_login) :
    ?>
    ~ display this for admin ~
    <?php
    else if('cabbage' == $user_login) :
    ?>
    ~ display this for cabbage ~
    <?php endif; ?>

    Probably I should figure out by myself… but I can’t 🙁
    What if I just want to check that somebody is logged in or not? Basically, to make it a “general” login check.

    moshu:

    <?php
    global $user_login;
    get_currentuserinfo();
    if($user_login) :
    ?>
    ~ display this if visitor is logged in ~
    <?php endif; ?>

    $user_login (or any var available through get_currentuserinfo()) will have no value, or rather provides a false return in this test, when you visit the site but are not logged in.

    Thanks, I always envy your PHP knowledge 🙂

    Thanks, I always envy your PHP knowledge

    ‘Twernt ‘nuthin. There are those I envy in the same way. ~:)

    Is there a way to make a link for only members with author & admin status?

    Kafka – many thanks for your advice, in your reply. Much appreciated 🙂

    Gordie–you can test for a user’s Capability with something like:

    if ($user->has_cap('edit_users'))

    or

    if ($user->has_cap('edit_posts'))

    or even

    if ($user->has_cap('level_8'))

    But, you asked for “author AND admin” so don’t know exactly how you would fit that in…?

    Regardless, taking off from Kaf’s example,

    <?php
    global $user_login,$user_ID;
    get_currentuserinfo();
    if($user_login) :
    ?>
    <?php
    $user = new WP_User( $user_ID );
    if ( $user->has_cap('edit_posts')) :
    ?>
    <?php echo 'this user has edit_posts capability'; ?>
    <?php endif; ?>
    <?php endif; ?>

    Don’t know if this is the most efficient way of doing it but it does work 😉

    Sounds like I could use this to display a link instead of wp_register (which flops between “Register” and, if you are registered and logged in, it says “Site Admin” which I think is a kind of obscure term, I’m gonna try to make it say “Post An Article” and point directly to Write Post page)

    thanks kafka!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Links for Admin only?’ is closed to new replies.
Skip to toolbar