grant_super_admin

函数
grant_super_admin ( $user_id )
参数
  • (int) $user_id ID of the user to be granted Super Admin privileges.
    Required:
返回值
  • (bool) True on success, false on failure. This can fail when the user is already a super admin or when the `$super_admins` global is defined.
定义位置
相关方法
get_super_adminsis_super_adminrevoke_super_adminis_user_admincurrent_user_can
引入
3.0.0
弃用
-

grant_super_admin: 这个函数用来授予WordPress网络中的一个用户超级管理员的权限: 该函数接收一个用户ID作为参数,并将该用户添加到超级管理员列表中。

授予超级管理员权限。

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;
}

常见问题

FAQs
查看更多 >