WP_Role::remove_cap( string $cap )

Removes a capability from a role.

Parameters

$capstringrequired
Capability name.

More Information

Changing the capabilities of a role is persistent, meaning the removed capability will stay in effect until explicitly granted.

This setting is saved to the database (in table wp_options, field 'wp_user_roles'), so you should run this only once, on theme/plugin activation and/or deactivation.

Source

public function remove_cap( $cap ) {
	unset( $this->capabilities[ $cap ] );
	wp_roles()->remove_cap( $this->name, $cap );
}

Changelog

VersionDescription
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    From Codex

    /**
     * Don't let editors read private posts.
     *
     * You should call the function when your plugin is activated.
     *
     * @uses WP_Role::remove_cap()
     */
    function remove_editor_read_private_posts() {
    
    	// get_role returns an instance of WP_Role.
    	$role = get_role( 'editor' );
    	$role->remove_cap( 'read_private_posts' );
    }
  2. Skip to note 6 content

    From Codex, Remove multiple capabilities example

    /**
     * Remove capabilities from editors.
     *
     * Call the function when your plugin/theme is activated.
     */
    function wpcodex_set_capabilities() {
    
        // Get the role object.
        $editor = get_role( 'editor' );
    
    	// A list of capabilities to remove from editors.
        $caps = array(
            'moderate_comments',
            'manage_categories',
            'manage_links',
            'edit_others_posts',
            'edit_others_pages',
            'delete_posts',
        );
    
        foreach ( $caps as $cap ) {
        
            // Remove the capability.
            $editor->remove_cap( $cap );
        }
    }
    add_action( 'init', 'wpcodex_set_capabilities' );

You must log in before being able to contribute a note or feedback.