WordPress.org

Ready to get started?Download WordPress

Forums

Links for Admin only? (12 posts)

  1. daverocketdavecouk
    Member
    Posted 8 years ago #

    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

  2. Kafkaesqui

    Posted 8 years ago #

    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; ?>

  3. croila
    Member
    Posted 8 years ago #

    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

  4. Kafkaesqui

    Posted 8 years ago #

    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; ?>

  5. moshu
    Member
    Posted 8 years ago #

    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.

  6. Kafkaesqui

    Posted 8 years ago #

    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.

  7. moshu
    Member
    Posted 8 years ago #

    Thanks, I always envy your PHP knowledge :)

  8. Kafkaesqui

    Posted 8 years ago #

    "Thanks, I always envy your PHP knowledge"

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

  9. Gordie
    Member
    Posted 8 years ago #

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

  10. croila
    Member
    Posted 8 years ago #

    Kafka - many thanks for your advice, in your reply. Much appreciated :-)

  11. MichaelH
    Member
    Posted 8 years ago #

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

  12. Dgold
    Member
    Posted 8 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.