grant_super_admin()WP 3.0.0

Sets the specified user as a super administrator.

"Super-administrator" is not a separate role like Editor, Administrator, but simply a set of permissions that expand the capabilities of the current role.

If a user had a role lower than "Administrator," for example, "Editor," then after granting them super-admin rights, they will gain the ability to manage everything.

This function adds the user's login to the site_admins option in the wp_sitemeta table:

$super_admins = get_site_option( 'site_admins' );

A super-admin can also be specified in the global variable $super_admins. If it is set, it takes precedence over the site_admins option, and this function becomes non-functional, with all management of super-admins transitioning to the global option $super_admins.

Use the function revoke_super_admin() to undo the action of this function (remove super-admin rights from the specified user).

Also, see WP-CLI commands related to super-admin: wp super-admin.

Hooks from the function

Returns

true|false.

  • true - Successfully made the user a super-admin.
  • false - Failed to make the user a super-admin.
    Or if the specified user is already a super-admin.
    Or if the global variable $super_admins is set.

Usage

grant_super_admin( $user_id );
$user_id(integer) (required)
User ID to which super-admin rights need to be granted.

Examples

0

#1 Give super admin rights (caps) to the user with ID 5

It is recommended to call the function only once, not every time the page is generated. Because it changes the value of the option.

grant_super_admin( 5 );
0

#2 Set super-admin rights (caps) and remove them when activating/deactivating the plugin

register_activation_hook( __FILE__, 'myplugin_activate' );
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

function myplugin_activate() {

	// give caps
	grant_super_admin( 5 );
}

function myplugin_deactivate(){

	// take away the caps
	revoke_super_admin( 5 );
}

Notes

  • Global. Array. $super_admins

Changelog

Since 3.0.0 Introduced.

grant_super_admin() code WP 7.0

function grant_super_admin( $user_id ) {
	// If global super_admins override is defined, there is nothing to do here.
	if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
		return false;
	}

	/**
	 * Fires before the user is granted Super Admin privileges.
	 *
	 * @since 3.0.0
	 *
	 * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
	 */
	do_action( 'grant_super_admin', $user_id );

	// Directly fetch site_admins instead of using get_super_admins().
	$super_admins = get_site_option( 'site_admins', array( 'admin' ) );

	$user = get_userdata( $user_id );
	if ( $user && ! in_array( $user->user_login, $super_admins, true ) ) {
		$super_admins[] = $user->user_login;
		update_site_option( 'site_admins', $super_admins );

		/**
		 * Fires after the user is granted Super Admin privileges.
		 *
		 * @since 3.0.0
		 *
		 * @param int $user_id ID of the user that was granted Super Admin privileges.
		 */
		do_action( 'granted_super_admin', $user_id );
		return true;
	}
	return false;
}