允许管理员角色访问自定义功能

时间:2012-08-21 作者:JamieCassidy

我目前正在开发一个Wordpress商业目录插件,其中包含一个“商业”自定义帖子类型。我让它在激活时创建一个自定义的“business\\u author”用户角色,并为该角色分配特定功能,但它似乎限制了“Administrator”角色访问“business”自定义帖子类型。我曾尝试在插件激活时将这些功能添加到“管理员”角色,但似乎不起作用。

p、 我正在使用“map\\u meta\\u cap”过滤器为该帖子类型创建自定义功能。

下面的代码示例。。。

register_activation_hook( __FILE__, \'tis_business_activation\' );
register_deactivation_hook( __FILE__, \'tis_business_deactivation\' );

function tis_business_activation() {
    add_role(\'business_author\', \'Business Listing Author\', array(
        \'publish_businesses\' => false,
        \'edit_businesses\' => true,
        \'edit_others_busineses\' => false,
        \'delete_businesses\' => true,
        \'delete_others_businesess\' => false,
        \'read_private_businesses\' => false,
        \'edit_business\' => true,
        \'delete_business\' => true,
        \'read_business\' => true,
        \'read\' => true,
        \'upload_files\' => true
    ));
//Attempting to add above capabilities to administrator role.
    $administrator = get_role(\'administrator\');
    $administrator->add_cap(\'publish_business\');
    $administrator->add_cap(\'edit_business\');
    $administrator->add_cap(\'edit_others_businesses\');
    $administrator->add_cap(\'delete_businesses\');
    $administrator->add_cap(\'delete_others_businesses\');
    $administrator->add_cap(\'read_private_businesses\');
    $administrator->add_cap(\'edit_business\');
    $administrator->add_cap(\'delete_business\');
    $administrator->add_cap(\'read_business\');
}

function tis_business_deactivation() {
    remove_role(\'business_author\');
    global $wp_roles;
    $wp_roles->remove_cap(\'administrator\', \'publish_business\');
    $wp_roles->remove_cap(\'administrator\', \'edit_business\');
    $wp_roles->remove_cap(\'administrator\', \'edit_others_businesses\');
    $wp_roles->remove_cap(\'administrator\', \'delete_businesses\');
    $wp_roles->remove_cap(\'administrator\', \'delete_others_businesses\');
    $wp_roles->remove_cap(\'administrator\', \'read_private_businesses\');
    $wp_roles->remove_cap(\'administrator\', \'edit_business\');
    $wp_roles->remove_cap(\'administrator\', \'delete_business\');
    $wp_roles->remove_cap(\'administrator\', \'read_business\');
}
任何帮助都会很神奇,我真的被这件事困住了。

抱歉,如果这已经得到了回答,我到处寻找答案,但找不到。

1 个回复
SO网友:Eric Holmes

参考Codex 对此,我建议使用另一个钩子。它们使用“admin\\u init”操作:

function add_theme_caps() {
    $role = get_role( \'author\' ); // gets the author role
     $role->add_cap( \'edit_others_posts\' ); // would allow the author to edit others\' posts for current theme only
}
add_action( \'admin_init\', \'add_theme_caps\');

结束

相关推荐

PHP致命错误:无法为wp-includes/capabilities.php中的非对象调用重载函数

我在apache日志中遇到了太多以下错误。PHP Fatal error: Cannot call overloaded function for non-object in wp-includes/capabilities.php on line 1187这是函数current\\u user\\u can($capability)的内部,第1187行如下所示:$current_user = wp_get_current_user(); 我不知道问题出在哪里?