How can you check if specific username is logged in?
1 Answer
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
}
}
-
1How 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
-
1The question is not about the current user, he means a specific user– Johnny97Commented 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!– LyallCommented Jan 15, 2020 at 11:09
-