Using WordPress ‘get_admin_users_for_domain()’ PHP function

The get_admin_users_for_domain() WordPress PHP function is used to retrieve the admin user(s) associated with a given domain/path combination in a multisite network.

Usage

A generic example of how to use the function is as follows:

$admin_users = get_admin_users_for_domain($domain, $path);

where $domain is an optional string parameter representing the network domain and $path is an optional string parameter representing the network path.

A custom example could be:

$admin_users = get_admin_users_for_domain('example.com', '/blog/');

This would retrieve the admin user(s) associated with the domain example.com and the path /blog/.

Parameters

  • $domain (string) – Optional. The network domain. Default is an empty string.
  • $path (string) – Optional. The network path. Default is an empty string.

More Information

See WordPress Developer Resources: get_admin_users_for_domain. This function was introduced in WordPress 3.0.0 and is not deprecated.

Examples

Here are some practical examples of how to use the get_admin_users_for_domain() function:

Retrieving admin user(s) for a given domain and path combination

$admin_users = get_admin_users_for_domain('example.com', '/blog/');
if (!empty($admin_users)) {
  echo 'The admin users for example.com/blog/ are: ';
  foreach ($admin_users as $user) {
    echo $user->user_login . ', ';
  }
}

This code retrieves the admin user(s) associated with the domain example.com and the path /blog/. If there are any admin users, it outputs their login names.

Retrieving admin user(s) for a specific domain

$admin_users = get_admin_users_for_domain('example.com');
if (!empty($admin_users)) {
  echo 'The admin users for example.com are: ';
  foreach ($admin_users as $user) {
    echo $user->user_login . ', ';
  }
}

This code retrieves the admin user(s) associated with the domain example.com. If there are any admin users, it outputs their login names.

Retrieving admin user(s) for a specific path

$admin_users = get_admin_users_for_domain('', '/blog/');
if (!empty($admin_users)) {
  echo 'The admin users for the path /blog/ are: ';
  foreach ($admin_users as $user) {
    echo $user->user_login . ', ';
  }
}

This code retrieves the admin user(s) associated with the path /blog/. If there are any admin users, it outputs their login names.

Checking if a user is an admin for a specific domain and path

$admin_users = get_admin_users_for_domain('example.com', '/blog/');
$is_admin = false;
foreach ($admin_users as $user) {
  if ($user->ID === get_current_user_id()) {
    $is_admin = true;
    break;
  }
}
if ($is_admin) {
  echo 'You are an admin for example.com/blog/.';
} else {
  echo 'You are not an admin for example.com/blog/.';
}

This code retrieves the admin user(s) associated with the domain example.com and the path /blog/, and checks if the currently logged in user is one of the admin users. If the user is an admin, it outputs a message indicating that.