仅允许作者编辑某些用户

时间:2013-07-05 作者:mantis

我试图让我的“作者”(我已将其重命名为“队长”)能够访问其团队成员的用户配置文件,并且仅访问其团队成员的用户配置文件。团队由用户元框定义(这比项目其余部分的用户分类法更有效)。作者已获得edit_users 能力。

我在想我可以这样做:

add_action(\'user_row_actions\',\'captains_user_row_actions\',10,2);

function captains_user_row_actions($actions, $user_object) {  // remove the ability to edit a non-team-member
$current_user = wp_get_current_user();
$cap_team_id = get_user_meta($current_user->ID, \'team-meta\', true);
$user_team_id = get_user_meta($user_object->ID, \'team-meta\', true);    

global $pagenow;
if ($pagenow==\'users.php\' && isset($user_object->caps[\'author\']) &&  $cap_team_id != $user_team_id )
unset($actions[\'edit\']);

return $actions;
}
要么这不是出路。或者我错过了什么。

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

I did not test the following code, 但它应该做你想做的事(或者至少为你指明正确的方向)。

function captains_user_row_actions($actions, $user) {
    // remove the ability to edit a non-team-member
    $cap_team_id = get_user_meta(wp_get_current_user()->ID, \'team-meta\', true);
    $user_team_id = get_user_meta($user->ID, \'team-meta\', true);
    if (\'users.php\' === $GLOBALS[\'pagenow\'] && $cap_team_id !== $user_team_id)
        unset($actions[\'edit\']);

    return $actions;
}
add_action(\'user_row_actions\', \'captains_user_row_actions\', 10, 2);
// EDIT
将以下内容添加到functions.php 还要处理直接编辑的文件:

function my_captain_func() {
    $cap_team_id = get_user_meta(wp_get_current_user()->ID, \'team-meta\', true);
    $user_team_id = get_user_meta($_GET[\'user_id\'], \'team-meta\', true);
    if ($cap_team_id !== $user_team_id && ! current_user_can(\'edit_pages\')) {
        wp_redirect(admin_url());   // or wherever you like
        exit;
    }
}   
if (\'user-edit.php\' === $GLOBALS[\'pagenow\'])
    add_action(\'wp_loaded\', \'my_captain_func\');

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?