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


8

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

我添加了一个具有Capability类型的新帖子类型,blog以及一个具有所有上限的新用户角色,这将允许管理员访问用户添加/编辑自定义帖子类型。这适用于管理员,他们可以在后端访问帖子类型。但是,具有我的自定义角色的用户根本无法进入后端。

注释的帖子类型参数

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

注册角色

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 );

加盖

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 );

我一直在疯狂地寻找原因。我尝试使用复数,非复数大写字母,尝试将功能添加到帖子类型的args中。但是,我永远无法进入后端。我在主题中没有任何其他代码可能会将用户赶出管理员(我删除了自己的代码,在测试时将他们踢出了管理员)

编辑 在这里,您可以从数据库中看到blog_manager功能的转储,那里还有很多测试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,
    ),
)

1
请注意,角色和功能会永久保存。如果您有一些不允许访问的早期版本,则它可能仍然是角色的一部分。转储持久性数据,查看是否有任何不应该设置的内容。
罗斯特(Rarst)

我添加了数据库功能的转储。我只有上面代码中设置为false的3个发布功能。
克里斯·莫里斯

我将安装用户角色编辑器插件-wordpress.org/plugins/user-role-editor。然后手动检查用户/角色之间的差异-可能存在某些冲突或缺失。
Welcher

嗨@ChrisMorris,您如何处理这个?您最终找到了解决方案吗?
蒂姆·马隆

Answers:


3

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

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

<?php
/**
 * Plugin Name: WPSE 186337
 * Description: Debug WordPress StackExchange question 186337
 * Plugin URI: /wordpress/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 );
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.