Wordpress - Check if user is admin by user ID

You worded it as you want to check if user has administrator role. It is considered more proper to check for capability, relevant to specific action.

In practice this is usually expressed as current_user_can( 'manage_options' ) for current user. For arbitrary user it would be user_can( $user_id, 'manage_options' ).

Of course this is capability that stands for general configuration of the site. Depending on your purpose you might want to use a different one, see list in roles and capabilities documentation.


You can use get_userdata() or get_user_by() by passing user id yo can get user object. From which you get role.

$user = get_userdata( $user_id );
if(!empty( $user ) && $user){
   $user->roles // this contains the role here check for whatever role you need
}

You can check this answer for more


I'm just adding to @Rarst answer.

Technically you can supply the role name to those functions, so current_user_can( 'administrator' ) or user_can( $user_id, 'administrator' ) would also work but it is discouraged.

While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

Note: Will always return true if the current user is a super admin, unless specifically denied.

You could check against the role without relying on those functions (since they are unreliable), but like @Rarst said, it is more proper to check for capabilities instead of role.

One main advantage to using capabilities instead of roles is as follow.

Take for instance that you have more than one administrator on your site. Let's say you want your client to be admin, and you are also admin of the site.

Now it makes sense that your client has admin privileges over his site, but you might not want him/her to have the ability to install plugins to the site. That would be your job as a webmaster.

So if you check on roles, you are stuck at disabling/enabling functionalities to everyone within that role.

Now if you use capabilities, you would have more granularity over granting/restricting users from doing specific actions.

Back to my scenario, if you want to give your client administrator privileges but not be able to install plugins, it would be as simple as

add_filter( 'user_has_cap', 'client_admin_cap_filter', 10, 3 );
function client_admin_cap_filter( $allcaps, $cap, $args ){

  // the requested capability and our client user id 
  if ( 'install_plugins' == $args[0] && $args[1] == 2 ) 
    $allcaps[ $cap[0] ] = false;
  
  return $allcaps;
}

we could supply an array of different capabilities we would want to restrict, for instance update_core, manage_options, install_plugins, install_themes etc.

Our function would still be pretty simple

add_filter( 'user_has_cap', 'client_admin_cap_filter', 10, 3 );
function client_admin_cap_filter( $allcaps, $cap, $args ){

  $restricted_cap = array( 
    'update_core',
    'manage_options',
    'install_plugins',
    'install_themes',
     // etc.
  );

  foreach( $restricted_cap as $r_cap ){

    // the requested capability and our client user id 
    if ( $r_cap == $args[0] && $args[1] == 2 ) 
      $allcaps[ $cap[0] ] = false;

  } 
  
  return $allcaps;
}

So as you can see, this is pretty effective, doesn't required a special role, doesn't add an extra DB query, is executed at runtime and is easily scalable and maintainable.

If you logic depends on a role, well good luck targeting specific users, scenarios or whatnots without pulling your hair out or creating a gazillion roles just to keep up with all possible use case.

Check the codex for the more info on the user_has_cap filter.