管理员的自定义角色功能未生效(无插件)

时间:2016-01-27 作者:Apple

我已经从插件创建了一个自定义帖子类型。我添加了一个功能类型teacher. 然后,我向administrator添加了自定义功能。但问题是,在完成这段代码后,它第一次将上述功能添加到角色中。但当我进一步修改它时,它不会生效。就像我现在删除函数一样roles_to_edit_teacher, 仍然管理员将具有这些功能。那么如何解决这个问题呢?

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 个回复
SO网友:cybmeta

功能存储在数据库中,这就是为什么即使您删除了功能,它们仍保持活动状态。需要明确删除功能:

$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\');
    }
}

相关推荐

未定义的偏移量:1067行的>[...]/wp-includes/capabilities.php中的0

嘿,我在我的localhost设置中得到了这个错误消息,但只有在启用Genesis框架的情况下;WordPress二十一行。当我想创建一个新帖子时,就会发生这种情况。如果我刷新页面,错误会重复,但帖子本身会被创建,一切似乎都很好。有人知道这是什么原因吗?Notice: Undefined offset: 0 in /var/www/secret/htdocs/wp-includes/capabilities.php on line 1067 Notice: Undefined offset: 0