1

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

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');
    }
}
  • Thanks #cybmeta , But I have already stored capabilities in the database by adding my code... and now modified it with your code.. but still it is working as like as before... may be I need to clear the database... Any Idea how to clear the database... – Apple Jan 27 '16 at 9:15
  • With my code you need to deactivate the plugin and reactivate again. Note that capabilities are being added on activation/deactivation hook. Have done it? – cybmeta Jan 27 '16 at 10:15
  • Yes, I have tried deactivating the plugin and then reactivate... but it doesn't work.... whatever I do with the code it just stays the same as before. – Apple Jan 28 '16 at 11:10
  • The code is working here and it is almost the same I use in several websites; your problem must be some where else, not in this code. – cybmeta Jan 28 '16 at 11:23

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.