功能存储在数据库中,这就是为什么即使您删除了功能,它们仍保持活动状态。需要明确删除功能:
$admin = get_role(\'author\');
$admin->remove_cap( \'edit_teacher\' );
而且,由于它们存储在数据库中,您应该停止在每次页面加载中添加它们(就像您在中所做的那样
init
事件)。通常,您应该在插件激活挂钩上添加自定义功能,并在插件停用时删除这些功能:
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();
} );
然后,如果需要在插件激活时在其他事件中修改功能,则应在添加/删除功能之前进行一些检查,以避免执行不必要的数据库操作。
例如:
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\');
}
}