允许自定义角色访问后端的自定义帖子类型

时间:2015-05-05 作者:Chris Morris

所以我对此有一些问题,我不明白为什么。我只需要一个自定义角色,可以访问后端的博客。

我添加了一个新的职位类型,其能力类型为blog 以及一个新的用户角色,该角色具有所有大写字母,允许管理员访问用户添加/编辑自定义帖子类型。这适用于管理员,他们可以在后端访问post类型。但是,我的自定义角色的用户根本无法进入后端。

Post type args of note

"capability_type" => \'blog\',
"map_meta_cap" => true,

Register role

function add_blog_manager_role(){
    add_role(
        \'blog_manager\',
        \'Blog Manager\',
        array(
            \'read\' => true,
            \'edit_posts\' => false,
            \'delete_posts\' => false,
            \'publish_posts\' => false,
            \'upload_files\' => true
        )
    );
}
add_action( \'admin_init\', \'add_blog_manager_role\', 4 );

Add Caps

function add_blog_role_caps() {
    $roles = array(\'blog_manager\', \'editor\',\'administrator\');
    foreach($roles as $the_role) {
        $role = get_role($the_role);
        $role->add_cap( \'read\' );
        $role->add_cap( \'read_blog\');
        $role->add_cap( \'read_private_blog\' );
        $role->add_cap( \'edit_blog\' );
        $role->add_cap( \'edit_others_blog\' );
        $role->add_cap( \'edit_published_blog\' );
        $role->add_cap( \'publish_blog\' );
        $role->add_cap( \'delete_others_blog\' );
        $role->add_cap( \'delete_private_blog\' );
        $role->add_cap( \'delete_published_blog\' );
    }
}
add_action(\'admin_init\', \'add_blog_role_caps\', 5 );
我一直在谷歌上疯狂搜索,试图找到这一切的原因。我尝试了复数、非复数大写,尝试了在post-type-args中添加功能。然而,我永远无法进入后端。我在主题中没有任何其他可能将用户踢出管理员的代码(我在测试时删除了自己将用户踢出管理员的代码)

Edit在这里,您可以看到数据库中的blog\\u管理器功能转储,还有很多测试BS,但这不应该阻止他们从我所知的位置登录。

\'blog_manager\' => array (
    \'name\' => \'Blog Manager\',
    \'capabilities\' => array (
        \'read\' => true,
        \'edit_posts\' => false,
        \'delete_posts\' => false,
        \'publish_posts\' => false,
        \'upload_files\' => true,
        \'read_blog\' => true,
        \'read_private_blog\' => true,
        \'edit_blog\' => true,
        \'edit_others_blog\' => true,
        \'edit_published_blog\' => true,
        \'publish_blog\' => true,
        \'delete_others_blog\' => true,
        \'delete_private_blog\' => true,
        \'delete_published_blog\' => true,
        \'blog\' => true,
        \'read_private_blogs\' => true,
        \'edit_blogs\' => true,
        \'edit_others_blogs\' => true,
        \'edit_published_blogs\' => true,
        \'publish_blogs\' => true,
        \'delete_others_blogs\' => true,
        \'delete_private_blogs\' => true,
        \'delete_published_blogs\' => true,
        \'delete_blogs\' => true,
        \'delete_blog\' => true,
    ),
)

2 个回复
SO网友:Nathan Johnson

很难对上述代码进行故障排除,因为它只是实际代码的一部分,但下面是注册自定义帖子类型(称为示例)和有权访问示例自定义帖子类型的自定义角色(博客管理器)所需的最小插件。

这可以用作主题功能的一部分。php文件。只需使用主题激活和停用挂钩即可。

<?php
/**
 * Plugin Name: WPSE 186337
 * Description: Debug WordPress StackExchange question 186337
 * Plugin URI: https://wordpress.stackexchange.com/questions/186337/
 * Author: Nathan Johnson
 * Licence: GPL2+
 * Licence URI: https://www.gnu.org/licenses/gpl-2.0.en.html
 */

//* Don\'t access this file directly
defined( \'ABSPATH\' ) or die();

//* Add action to init to register custom post type
add_action( \'init\', \'se186337_init\' );

//* Register activation hook to add Blog Manager role
register_activation_hook( __FILE__ , \'se186337_activation\' );

//* Register deactivation hook to remove Blog Manager role
register_deactivation_hook( __FILE__ , \'se186337_deactivation\' );

function se186337_activation() {
  $caps = [
    //* Meta capabilities
    \'read\'                   => true,
    \'edit_blog\'              => true,
    \'read_blog\'              => true,
    \'delete_blog\'            => true,

    //* Primitive capabilities used outside of map_meta_cap()
    \'edit_blogs\'             => true,
    \'edit_others_blogs\'      => true,
    \'publish_blogs\'          => true,
    \'read_private_blogs\'     => true,

    //* Primitive capabilities used within of map_meta_cap()
    \'delete_blogs\'           => true,
    \'delete_private_blogs\'   => true,
    \'delete_published_blogs\' => true,
    \'delete_others_blogs\'    => true,
    \'edit_private_blogs\'     => true,
    \'edit_published_blogs\'   => true,
  ];

  add_role( \'blog_manager\', \'Blog Manager\', $caps );
}

function se186337_deactivation() {
  remove_role( \'blog_manager\' );
}

function se186337_init() {
  $labels = [
    \'name\'          => __( \'Examples\' ),
    \'singular_name\' => __( \'Example\' ),
  ];
  $args = [
    \'labels\'          => $labels,
    \'public\'          => true,
    \'has_archive\'     => true,
    \'capability_type\' => \'blog\',
    \'map_meta_cap\'    => true,
  ];
  register_post_type( \'examples\', $args );
}

SO网友:Akshay Shah

如果你对插件没问题,那么你可以使用velow插件。

https://wordpress.org/plugins/capability-manager-enhanced/

结束

相关推荐

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