How to test for Super Admin

I have the following code in my mu-plugins.php file and the is_super_admin() function does not correctly result in true.

I am running v4.2.4

function check_for_superAdmin() {
    if ( is_super_admin() ) { echo 'I\'m a Super Admin !'; exit; }
}
add_action( 'wp_login', 'check_for_superAdmin' );

Can anyone see a reason why?

Notes:

  • I login at http://example.com/wp-admin/network
  • I even try logging in at http://example.com/wp-admin
  • the user is tagged as Super Admin in the Network Admin > Users list

Interesting

The code below returns empty for the current user login username. I wonder if I have my hook in the wrong place.

function check_for_superAdmin() {
    $curUser = wp_get_current_user();
    echo $curUser->user_login . ' is the user';
    exit;
    if ( is_super_admin() ) { echo 'I am the Super Admin !'; exit; }
}
add_action( 'wp_login', 'check_for_superAdmin' );

Additional Notes:

function check_for_superAdmin($user_login, $user) {
    //$current_user = wp_get_current_user(); // THIS DOES NOT FIND THE CURRENT USER
    //echo $user_login; // THIS ACCURATELY SHOWS THE LOGGED IN USER
    //echo $user; // THIS THROWS A SERVER 500 ERROR
}
add_action( 'wp_login', 'check_for_superAdmin', 10, 2 );
  • $current_user = wp_get_current_user(); does not work within the function() above
  • $user_login works within the function() above
  • $user results in a server 500 error

1 Answer
1

I got it to work as follows — in mu-plugins directory, my superAdmin.php script looks like this:

<?php

function check_for_superAdmin($user_login, $user) {
    $current_user = get_userdatabylogin($user_login);
    if ( is_super_admin( $current_user->ID ) ) { // do these things }
    else { // do other things }
}
add_action( 'wp_login', 'check_for_superAdmin', 10, 2 );

?>

Hope this helps someone wanting to hook the same way.

Leave a Comment