4

How can you check if specific username is logged in?

1
  • I'd like to use specific css on the admin panel for a specific user.
    – Shae
    Commented Nov 29, 2012 at 22:31

1 Answer 1

12

wp_get_current_user then compare the object that's returned user_login proprety with the specific username you'd like to check against.

<?php
add_action('admin_init', 'wpse74389_check_username');
function wpse74389_check_username()
{
    $user = wp_get_current_user();

    if($user && isset($user->user_login) && 'username_to_check' == $user->user_login) {
        // do stuff
    }
}
5
  • 1
    How can this be modified to check for all usernames that are not username_to_check Commented Nov 27, 2018 at 5:19
  • @TravisPflanz if you're just looking to check for a logged in user: developer.wordpress.org/reference/functions/is_user_logged_in Otherwise just modify the function to take the username as a parameter instead of being hardcoded. Commented Nov 30, 2018 at 20:08
  • 1
    The question is not about the current user, he means a specific user
    – Johnny97
    Commented Jul 15, 2019 at 18:03
  • @chrisguitarguy Please can you advise how to change this to check if the username is in an array of usernames, rather than just one? They can still be hard coded as per your example. Thanks!
    – Lyall
    Commented Jan 15, 2020 at 11:09
  • Use in_array instead of a simple equality check. Commented Jan 15, 2020 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.