Custom role capabilities to administrator not taking effect (no plugin)

I have created a custom post type from a plugin. I added a capability type teacher. Then I have added custom capabilities to administrator. But the problem is for the first time after completing this code it adds mentioned capabilities to the role. But when I change it further, it doesn’t take effect. Like if I now delete the function roles_to_edit_teacher, still administrator will have the capabilities. So how to solve this issue?

function plugin_main_functions(){
register_post_type('teachers', array(
    'labels' => array(
        'name'=>__('Teachers List', 'Teachers'),
        'add_new' => __('add new teacher', 'Teachers'),
        'add_new_item' => __('Add new teacher', 'Teachers')
        ),
    'public' => true,
    'supports'=> array('title', 'editor', 'thumbnail'),
    'menu_icon' => 'dashicons-groups',
    'capability_type' => array('teacher','teachers'),
    'map_meta_cap' => true

    ));

}
add_action('init','plugin_main_functions');

function roles_to_edit_teacher(){

    $admin = get_role('administrator');


    $admin->add_cap('edit_teacher');
    $admin->add_cap('edit_teachers');
    $admin->add_cap('read_teacher');
    $admin->add_cap('delete_teacher');
    $admin->add_cap('delete_teachers');
    $admin->add_cap('publish_teacher');
    $admin->add_cap('create_teachers');
}
add_action('init', 'roles_to_edit_teacher');

1 Answer
1

Capabilities are stored in database, that is why they keep active even if you remove your function. Capabilities need to be expressly removed:

$admin  = get_role('author');
$admin->remove_cap( 'edit_teacher' );

And, because they are stored in database, you should stop adding them in every page load (as you are doing in init event). Usually, you should add custom capabilities on plugin activation hook and remove them on plugin deactivation:

add_action('init','plugin_main_functions');
function plugin_main_functions(){
  register_post_type('teachers', array(
    'labels' => array(
        'name'=>__('Teachers List', 'Teachers'),
        'add_new' => __('add new teacher', 'Teachers'),
        'add_new_item' => __('Add new teacher', 'Teachers')
        ),
    'public' => true,
    'supports'=> array('title', 'editor', 'thumbnail'),
    'menu_icon' => 'dashicons-groups',
    'capability_type' => array('teacher','teachers'),
    'map_meta_cap' => true

  ));

}

register_activation_hook( __FILE__, function () {

    // Call function plugin_main_functions(),
    // The function where the custom post type is registered
    plugin_main_functions();

    $admin = get_role('administrator');

    $admin->add_cap('edit_teacher');
    $admin->add_cap('edit_teachers');
    $admin->add_cap('read_teacher');
    $admin->add_cap('delete_teacher');
    $admin->add_cap('delete_teachers');
    $admin->add_cap('publish_teacher');
    $admin->add_cap('create_teachers');

    flush_rewrite_rules();

} );

register_deactivation_hook( __FILE__, function () {

    // Call function plugin_main_functions(),
    // The function where the custom post type is registered
    plugin_main_functions();

    $admin = get_role('administrator');

    $admin->remove_cap('edit_teacher');
    $admin->remove_cap('edit_teachers');
    $admin->remove_cap('read_teacher');
    $admin->remove_cap('delete_teacher');
    $admin->remove_cap('delete_teachers');
    $admin->remove_cap('publish_teacher');
    $admin->remove_cap('create_teachers');

    flush_rewrite_rules();

} );

Then, if you need to modify the capabilities in a different event while the plugin is activate, you should do some checks before adding/removing capabilities so you don’t perfrom unnecessary database operations.

For example:

add_action('init','plugin_main_functions');
function roles_to_edit_teacher(){
    if( $i_neet_to_remove_cap === true ) {
        $admin = get_role('administrator');
        $admin->remove_cap('some_capability');
    }
    if( $i_neet_to_add_cap === true ) {
        $admin = get_role('administrator');
        $admin->add_cap('some_capability');
    }
}

Leave a Comment