自定义帖子类型角色权限不会让我阅读


9

我无法获得它,因此我作为供应商的用户角色可以读取货件的张贴类型。它显示在他们的菜单中,但是当您单击它时,您将无法查看此页面错误消息。

如果我可以,add_cap('read_posts')但我不希望他们仅查看装运帖子类型,则可以正常查看。

我具有以下功能的供应商用户角色。

WP_Role Object
(
    [name] => supplier
    [capabilities] => Array
        (
            [read] => 1
            [edit_shipment] => 1
            [read_shipment] => 1
            [edit_others_shipments] => 1
            [publish_shipments] => 1
            [read_private_shipments] => 1
            [edit_shipments] => 1
            [create_shipment] => 1
            [read_shipments] => 1
        )

)

我已经为运输设置了以下类型

function shipment_post_type() {
  $labels = array(
    'name'                  => _x( 'Shipments', 'Post Type General Name', 'sage' ),
    'singular_name'         => _x( 'Shipment', 'Post Type Singular Name', 'sage' ),
    'menu_name'             => __( 'Shipments', 'sage' ),
    'name_admin_bar'        => __( 'Shipments', 'sage' ),
    'archives'              => __( 'Shipment Archives', 'sage' ),
    'parent_item_colon'     => __( 'Parent shipment:', 'sage' ),
    'all_items'             => __( 'All shipments', 'sage' ),
    'add_new_item'          => __( 'Add New shipment', 'sage' ),
    'new_item'              => __( 'New shipment', 'sage' ),
    'edit_item'             => __( 'Edit shipment', 'sage' ),
    'update_item'           => __( 'Update shipment', 'sage' ),
    'view_item'             => __( 'View shipment', 'sage' ),
    'search_items'          => __( 'Search shipments', 'sage' ),
    'not_found'             => __( 'Not found', 'sage' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'sage' ),
    'featured_image'        => __( 'Featured Image', 'sage' ),
    'set_featured_image'    => __( 'Set shipment image', 'sage' ),
    'remove_featured_image' => __( 'Remove shipment image', 'sage' ),
    'use_featured_image'    => __( 'Use as shipment image', 'sage' ),
    'insert_into_item'      => __( 'Insert into shipment', 'sage' ),
    'uploaded_to_this_item' => __( 'Uploaded to this shipment', 'sage' ),
    'items_list'            => __( 'shipments list', 'sage' ),
    'items_list_navigation' => __( 'Constests list navigation', 'sage' ),
    'filter_items_list'     => __( 'Filter shipments list', 'sage' ),
  );
  $args = array(
    'label'                 => __( 'shipments', 'sage' ),
    'description'           => __( 'Manage all shipments, sweepstakes and giveaways.', 'sage' ),
    'labels'                => $labels,
    'supports'              => array( 'revisions' ),
    'taxonomies'            => array( '' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-archive',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => false,
    'can_export'            => true,
    'has_archive'           => false,
    'exclude_from_search'   => true,
    'publicly_queryable'    => true,
    'map_meta_cap' => true,
    'capabilities' => array(
      'edit_post'          => 'edit_shipment',
      'read_post'          => 'read_shipment',
      'read_posts'         => 'read_shipments',
      'delete_post'        => 'delete_shipment',
      'delete_posts'       => 'delete_shipments',
      'edit_posts'         => 'edit_shipments',
      'edit_others_posts'  => 'edit_others_shipments',
      'publish_posts'      => 'publish_shipments',
      'read_private_posts' => 'read_private_shipments',
      'create_posts'       => 'create_shipments',
    ),
  );
  register_post_type( 'shipment', $args );

}
add_action( 'init', 'shipment_post_type', 0 );


我已经读过很多遍了……按照说明可以做的一切。仍然得到您的权限,直到我授予create_shipments权限。
Nicholas Koskowski

3
您可以在添加角色add_role()和上限的位置共享您的代码add_cap()吗?

1
您是要让供应商在前端还是在后端读取此帖子类型?
Myles

这段代码是在插件还是主题中?插件更好,但是无论哪种方式,只要提供有关您的设置的更多信息,我相信我都可以为您提供更全面的解决方案
Myles

Answers:


4

您的自定义帖子类型看起来设置正确。它适用于我的测试安装。尝试此操作,而不要使用您当前正在使用的add_role和add_cap代码。(仅出于测试目的。出于以下概述的原因,请勿在生产代码中使用它。)它对我有用:

function prefix_set_up_supplier_role(){
remove_role( 'supplier' );
add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}
add_action( 'init', 'prefix_set_up_supplier_role' );

请记住,添加用户角色和功能实际上会将数据保存到数据库中,这一点非常重要。因此,如果您之前的代码版本无法正常运行,然后添加了可以使其正常工作的代码,那么如果数据库中仍然有旧数据,则该代码可能不会生效。如果角色已经存在于数据库中,则add_role()返回null。对于生产代码,您实际上应该对此使用插件激活和停用钩子,而不是每次都运行它,如下所示:

register_activation_hook( __FILE__, 'prefix_activate' );
function prefix_activate(){
    add_role( 'supplier', 'Supplier', array(
        'read'                      => true,
        'edit_shipment'             => true,
        'read_shipment'             => true,
        'read_shipments'            => true,
        'delete_shipment'           => true,
        'delete_shipments'          => true,
        'edit_shipments'            => true,
        'edit_others_shipments'     => true,
        'publish_shipments'         => true,
        'read_private_shipments'    => true,
        'create_shipments'          => true,
    )
);
}

register_deactivation_hook( __FILE__, 'prefix_deactivate' );
function prefix_deactivate(){
    remove_role( 'supplier' );
}

2
add_role()并且remove_role()不应在每个请求中都执行。请注意,一旦将角色添加到数据库,就无需再次添加它。通常,您register_activation_hookregister_deactivation_hook/ 上添加角色而在/ 上删除角色register_unistall_hook;除此之外,仅在确实需要时才应执行remove_role()/ add_role()
cybmeta

我知道,我在回答中也提到了这一点。但这仍然是诊断开发中问题的快速方法,因此您不必单击停用即可在每次对代码进行更改时都将其激活。然后,当它的工作,将其移动到激活和失活钩
迈尔斯

1
@cybmeta是的。我编辑了答案,以反映如何遵循最佳实践。
Myles

0

在您的角色对象中,您具有功能“ create_shipment”,实际上它应该说“ create_shipments”。似乎无论您在何处添加该功能,代码中都可能缺少“ s”。


0

您可以尝试:

add_action( 'init', 'add_my_caps');
function add_my_caps() {
    global $wp_roles;

    if ( isset($wp_roles) ) {
        $wp_roles->add_cap( 'editor', 'edit_shipment' );
        $wp_roles->add_cap( 'editor', 'read_shipment' );
        $wp_roles->add_cap( 'editor', 'delete_shipment' );
        $wp_roles->add_cap( 'editor', 'publish_shipments' );
        $wp_roles->add_cap( 'editor', 'edit_shipments' );
        $wp_roles->add_cap( 'editor', 'edit_others_shipments' );
        $wp_roles->add_cap( 'editor', 'delete_shipments' );
        $wp_roles->add_cap( 'editor', 'delete_others_shipments' );
        $wp_roles->add_cap( 'editor', 'read_private_shipments' );
        ....

0

试试看 'dms_document'是自定义帖子类型。

function jgd_add_role_caps() {
    // Add the roles you'd like to administer the custom post types
    $roles = array(
        'deity_user',
        'admin_user'
    );

    // Loop through each role and assign capabilities
    foreach($roles as $the_role) { 
        $role = get_role($the_role);

        $role->add_cap('read');
        $role->add_cap('read_dms_document');
        $role->add_cap('read_private_dms_documents');
        $role->add_cap('edit_dms_document');
        $role->add_cap('edit_dms_documents');
        $role->add_cap('edit_others_dms_documents');
        $role->add_cap('edit_published_dms_documents');
        $role->add_cap('publish_dms_documents');
        $role->add_cap('delete_others_dms_documents');
        $role->add_cap('delete_private_dms_documents');
        $role->add_cap('delete_dms_documents');
        $role->add_cap('delete_post_dms_documents');
        $role->add_cap('delete_published_dms_documents');
        $role->add_cap('delete_draft_dms_documents');
        $role->add_cap('delete_others_posts_dms_documents');
        $role->add_cap('delete_others_posts_dms_document');
        $role->add_cap('delete_posts_dms_documents');
        $role->add_cap('delete_posts_dms_document');
    }
}
add_action('admin_init','jgd_add_role_caps', 999);
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.