Allow authors to edit only certain users

I did not test the following code, but it should do what you want (or point you in the right direction, at least).

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
Add the following to your functions.php file to also handle direct editing:

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');

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)