如何让站点管理员在WordPress网络/多站点设置中编辑用户?

时间:2011-06-28 作者:cwd

我在网络中有几个站点。我不希望站点管理员管理网络插件之类的东西,但我希望他们能够编辑所有网络用户的个人资料,而不是使用他们站点上的个人资料。

/wp-admin/network/users.php 文件如下:

if ( ! current_user_can( \'manage_network_users\' ) )
    wp_die( __( \'You do not have permission to access this page.\' ) );
如何授予站点管理员权限manage_network_users 没有把他们提升为超级管理员?

代码如下所示:


/**
 * Retrieve a list of super admins.
 *
 * @since 3.0.0
 *
 * @uses $super_admins Super admins global variable, if set.
 *
 * @return array List of super admin logins
 */
function get_super_admins() {
    global $super_admins;

    if ( isset($super_admins) )
        return $super_admins;
    else
        return get_site_option( \'site_admins\', array(\'admin\') );
}

/**
 * Determine if user is a site admin.
 *
 * @since 3.0.0
 *
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 * @return bool True if the user is a site admin.
 */
function is_super_admin( $user_id = false ) {
    if ( $user_id )
        $user = new WP_User( $user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user->id ) )
        return false;

    if ( is_multisite() ) {
        $super_admins = get_super_admins();
        if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
            return true;
    } else {
        if ( $user->has_cap(\'delete_users\') )
            return true;
    }

    return false;
}

    /**
     * Whether user has capability or role name.
     *
     * This is useful for looking up whether the user has a specific role
     * assigned to the user. The second optional parameter can also be used to
     * check for capabilities against a specfic post.
     *
     * @since 2.0.0
     * @access public
     *
     * @param string|int $cap Capability or role name to search.
     * @param int $post_id Optional. Post ID to check capability against specific post.
     * @return bool True, if user has capability; false, if user does not have capability.
     */
    function has_cap( $cap ) {
#       fb($cap);
        if ( is_numeric( $cap ) ) {
            _deprecated_argument( __FUNCTION__, \'2.0\', __(\'Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.\') );
            $cap = $this->translate_level_to_cap( $cap );
        }

        $args = array_slice( func_get_args(), 1 );
        $args = array_merge( array( $cap, $this->ID ), $args );
        $caps = call_user_func_array( \'map_meta_cap\', $args );

        // Multisite super admin has all caps by definition, Unless specifically denied.
        if ( is_multisite() && is_super_admin( $this->ID ) ) {
            if ( in_array(\'do_not_allow\', $caps) )
                return false;
            return true;
        }

        // Must have ALL requested caps
        $capabilities = apply_filters( \'user_has_cap\', $this->allcaps, $caps, $args );
        $capabilities[\'exist\'] = true; // Everyone is allowed to exist
        foreach ( (array) $caps as $cap ) {
            //echo "Checking cap $cap
"; if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] ) return false; } return true; }
来自用户的代码。wp admin中的php显示编辑网络用户的第二个功能检查:

// Only allow super admins on multisite to edit every user.
if ( is_multisite() && ! current_user_can( \'manage_network_users\' ) && $user_id != $current_user->ID && ! apply_filters( \'enable_edit_any_user_configuration\', true ) )
    wp_die( __( \'You do not have permission to edit this user.\' ) );

3 个回复
最合适的回答,由SO网友:Michael 整理而成

以下是一个对我有效的解决方案:

http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/

非常优雅,不需要弄乱核心。

SO网友:Aurovrata

不幸的是,我没有足够的分数来添加评论或投票@Michael\'但是我想在另一篇文章中指出,这篇文章为阻止非管理员用户编辑管理员/超级管理员帐户的问题提供了更好的解决方案。

岗位解决方案link given by Michael works fine, 我刚刚将其应用于WP多站点v4。1、然而die() 非管理员用户尝试编辑管理员帐户时的函数。所以我用this solution (最初用于WP的单站点安装),当非管理员用户访问时,会从用户列表中隐藏所有管理员用户帐户。

最后的解决方案,将上述两种方法结合起来,给出了。。。

function mc_admin_users_caps( $caps, $cap, $user_id, $args ){

    foreach( $caps as $key => $capability ){

        if( $capability != \'do_not_allow\' )
            continue;

        switch( $cap ) {
            case \'edit_user\':
            case \'edit_users\':
                $caps[$key] = \'edit_users\';
                break;
            case \'delete_user\':
            case \'delete_users\':
                $caps[$key] = \'delete_users\';
                break;
            case \'create_users\':
                $caps[$key] = $cap;
                break;
        }
    }

    return $caps;
}
add_filter( \'map_meta_cap\', \'mc_admin_users_caps\', 1, 4 );
remove_all_filters( \'enable_edit_any_user_configuration\' );
add_filter( \'enable_edit_any_user_configuration\', \'__return_true\');

/*
 * hide admin from user list
 */
add_action(\'pre_user_query\',\'isa_pre_user_query\');
function isa_pre_user_query($user_search) {
  $user = wp_get_current_user();
  if ($user->ID!=1) { // Is not administrator, remove administrator
    global $wpdb;
    $user_search->query_where = str_replace(\'WHERE 1=1\',
      "WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
  }
}

SO网友:Dominic Tan

上面的代码仅隐藏ID为1的超级管理员。下面是隐藏所有超级用户的代码:

/*
 * hide super admin from user list
 */
add_action(\'pre_user_query\',\'isa_pre_user_query\');
function isa_pre_user_query($user_search) {
  $user = wp_get_current_user();
  if ( !is_super_admin($user->ID) ) { // Is not administrator, remove administrator
    global $wpdb;
        $admins = get_network_option(1, \'site_admins\', [1]);
        $admin_user = implode("\',\'", $admins);
    $user_search->query_where = str_replace(\'WHERE 1=1\', "WHERE 1=1 AND {$wpdb->users}.user_login NOT IN (\'{$admin_user}\')", $user_search->query_where);
  }
}

结束

相关推荐

wp-admin slow in multisite

使用子域多站点运行Wordpress 3.1.2(最新更新,大约一年前作为3.0安装);使用Sunrise域映射插件。前端速度很快,后端(wp admin)在网络管理站点(www.example.com/wp admin)上速度很慢,但在其中一个子域后端(foo.example.com/wp admin)上以“正常速度”运行。服务器是我们自己的,Ubuntu 10.04带有mod\\u安全性(我听说它可以减慢速度)。我能做些什么来加速后端或以某种方式运行跟踪吗?