0

I have added the code below to a plugin:

add_action( 'admin_init', 'remove_add_users_cap' );
function remove_add_users_cap() {
    $role = get_role( 'administrator' );
    $role->remove_cap( 'add_users' );
    $role->remove_cap( 'create_users' );
}

And I can see that the caps are no longer on my user:

if(!function_exists('wp_get_current_user')) {
    include(ABSPATH . "wp-includes/pluggable.php"); 
}
$current_user = wp_get_current_user();
echo '<pre>';
print_r($current_user);
echo '</pre>';

But I can still add new users O.o ...

Is there something else that needs to be done to achieve this? I would like to completely remove the ability for anyone to add a new user when this plugin is activated...

This is in a Wordpress Multisite context, and I have a feeling that may be complicating things.

Thanks,

  • Incidentally, from the codex: "This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this only once, probably on theme/plugin activation or deactivation" – vancoder Apr 2 '13 at 22:07
1

Okay, so the code above does work, but not for super admins, you must explicitly remove permissions from super admins, like this:

// restrict super admin capabilites
add_filter('map_meta_cap', 'restrict_super_admins', 10, 4);
function restrict_super_admins($caps, $cap, $user_id, $args){
    $super = array(
        'add_users',
        'create_users',
    );
    if( in_array($cap, $super) ) {
        $caps[] = 'do_not_allow';
    }
    return $caps;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.