将自定义角色功能应用于管理员(无插件)

时间:2012-12-04 作者:kristina childs

我有一个自定义角色,只允许访问单个自定义帖子类型。这一切都很好,但现在它只显示了这个角色,而不是管理员和超级管理员。我很难让它显示在管理仪表板上。

add_role( \'artists_relations\', \'Artist Relations\', array( \'post_artists\' ) );
function add_theme_caps() {
    $role = get_role( \'artists_relations\');
    $role->add_cap(\'delete_artists\');
    $role->add_cap(\'delete_published_artists\');
    $role->add_cap(\'delete_others_artists\');
    $role->add_cap(\'edit_artists\');
    $role->add_cap(\'edit_published_artists\');
    $role->add_cap(\'edit_others_artists\');
    $role->add_cap(\'publish_artists\');
    $role->add_cap(\'read\');
    $role->add_cap(\'upload_files\');
    $role->add_cap(\'manage_artist_categories\');
}
add_action( \'admin_init\', \'add_theme_caps\');
效果很好。我尝试了许多来自互联网的建议,但都没有奏效。不过,最近的尝试是这样的

function add_admin_caps() {
    $role = get_role( \'administrator\');
    $role->add_cap(\'delete_artists\');
    $role->add_cap(\'delete_published_artists\');
    $role->add_cap(\'delete_others_artists\');
    $role->add_cap(\'edit_artists\');
    $role->add_cap(\'edit_published_artists\');
    $role->add_cap(\'edit_others_artists\');
    $role->add_cap(\'publish_artists\');
    $role->add_cap(\'read\');
    $role->add_cap(\'upload_files\');
    $role->add_cap(\'manage_artist_categories\');
}
add_action( \'admin_init\', \'add_admin_caps\');
我读过几篇文章提到删除过滤器。我不明白为什么这对一个新角色有用,但不让我扩充现有的角色。但是,难道管理员不应该拥有所有的权限,而不必指定它吗?

--编辑--(添加post类型注册)

function register_artists_post_type() {
    register_post_type(\'artists\',array(
        \'labels\' => array(
            \'name\' => __( \'Artists\' ),
            \'singular_name\' => __( \'Artists\' ),
            \'add_new\' => __( \'Add Artist\',\'Artist\' ),
            \'add_new_item\' => __( \'Add New Artist\' ),
            \'edit_item\' => __( \'Edit Artist\' ),
            \'new_item\' => __( \'New Artist\' ),
            \'view_item\' => __( \'View Artist\' ),
            \'search_items\' => __( \'Search Artists\' ),
            \'not_found\' => __( \'No Artists Found\' ),
            \'not_found_in_trash\' => __( \'No Artists In Trash\' ),
            \'parent_item_colon\' => \'\'
        ),
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'has_archive\' => true,
        \'supports\' => array( \'title\',\'editor\',\'excerpt\',\'custom-fields\',\'thumbnail\' ),
        \'rewrite\' => array(\'slug\' => \'artists\',\'with_front\' => false),
        \'taxonomies\' => array(\'large_feature\',\'small_feature\'),
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'capabilities\' => array(
            \'publish_posts\' => \'publish_artists\',
            \'edit_posts\' => \'edit_artists\',
            \'edit_others_posts\' => \'edit_others_artists\',
            \'delete_posts\' => \'delete_artists\',
            \'delete_others_posts\' => \'delete_others_artists\',
            \'read_private_posts\' => \'read_private_artists\',
            \'edit_post\' => \'edit_artists\',
            \'delete_post\' => \'delete_artists\',
            \'read_post\' => \'read_artists\',
            \'manage_categories\' => \'manage_artist_categories\',
        )
    ));
}
add_action(\'init\',\'register_artists_post_type\');

1 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

首先,@Wyck是对的,看看你的register_post_type 密码如果是我,我会确保在$args 阵列:

register_post_type( ... array(
    \'capability_type\' => \'artists\',
    \'map_meta_cap\' => true
) );
接下来,您不想在每次管理页面加载时重置功能,这对您的服务器来说是不必要的工作。我使用的一个简单技巧是添加?reload_caps=1 之后/wp-admin/ 在我的主题中检查functions.php 文件以下是我在网站上成功使用的一些代码,已修改为使用角色artists_relations 和能力类型artists:

if ( is_admin() && \'1\' == $_GET[\'reload_caps\'] ) {
    $administrator     = get_role(\'administrator\');
    $artists_relations = get_role(\'artists_relations\');

    $administrator->add_cap( \'assign_custom_taxes\' );
    $artists_relations->add_cap( \'assign_custom_taxes\' );

    foreach ( array(\'publish\',\'delete\',\'delete_others\',\'delete_private\',\'delete_published\',\'edit\',\'edit_others\',\'edit_private\',\'edit_published\',\'read_private\') as $cap ) {
        $administrator->add_cap( "{$cap}_artists" );
        $artists_relations->add_cap( "{$cap}_artists" );
    }
}

结束

相关推荐

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(); 我不知道问题出在哪里?