登录自定义用户角色后,在 Wordpress 中创建自定义用户角色会完全隐藏帖子类型

时间:2021-02-24 21:52:14

标签: php wordpress

我不久前问过这个问题Wordpress custom user roles edit others posts?

我改变了我的方法,但它仍然不起作用。

我一直在尝试创建只能编辑特定帖子类型的自定义用户角色。

我的参考:https://wordpress.stackexchange.com/questions/96910/restrict-custom-post-type-view-by-user-role

根据附加的答案,我正在传递能力类型和能力。帖子类型的注册如下所示:

<?php

function engineering() {

    $labels = array(
        'name'                  => _x( 'Engineering Blog Posts', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Engineering Blog Posts', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Engineering Blog Posts', 'text_domain' ),
        'name_admin_bar'        => __( 'Engineering Blog Posts', 'text_domain' ),
        'archives'              => __( 'Engineering Blog Archives', 'text_domain' ),
        'attributes'            => __( 'Engineering Blog Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Engineering Blog Item:', 'text_domain' ),
        'all_items'             => __( 'All Engineering Blog Posts', 'text_domain' ),
        'add_new_item'          => __( 'Add New Engineering Blog Post', 'text_domain' ),
        'add_new'               => __( 'Add New Engineering Blog Post', 'text_domain' ),
        'new_item'              => __( 'New Engineering Blog Post', 'text_domain' ),
        'edit_item'             => __( 'Edit Engineering Blog Post', 'text_domain' ),
        'update_item'           => __( 'Update Engineering Blog Post', 'text_domain' ),
        'view_item'             => __( 'View Engineering Blog Post', 'text_domain' ),
        'view_items'            => __( 'View Engineering Posts', 'text_domain' ),
        'search_items'          => __( 'Search Engineering Blog Posts', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Engineering Blog', 'text_domain' ),
        'description'           => __( 'Engineering Blog', 'text_domain' ),
        'labels'                => $labels,
        'show_in_rest'          => true,
        'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' ),
        'taxonomies'            => array( 'engineering_taxonomy' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-media-code',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'engineering',
        'capabilities' => array(
            'publish_posts' => 'publish_engineerings',
            'edit_posts' => 'edit_engineerings',
            'edit_others_posts' => 'edit_others_engineerings',
            'read_private_posts' => 'read_private_engineerings',
            'edit_post' => 'edit_engineering',
            'delete_post' => 'delete_engineering',
            'read_post' => 'read_engineering',
        ),
        'rewrite' => array('slug' => 'engineering-blog/post', 'with_front' => false)
    );
    register_post_type( 'engineering', $args );

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


// Register Custom Taxonomy
function engineering_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Categories', 'text_domain' ),
        'all_items'                  => __( 'All Categories', 'text_domain' ),
        'parent_item'                => __( 'Parent Category', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Category:', 'text_domain' ),
        'new_item_name'              => __( 'New Category Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Category', 'text_domain' ),
        'edit_item'                  => __( 'Edit Category', 'text_domain' ),
        'update_item'                => __( 'Update Category', 'text_domain' ),
        'view_item'                  => __( 'View Category', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate categories with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove categories', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular categories', 'text_domain' ),
        'search_items'               => __( 'Search Categories', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No categories', 'text_domain' ),
        'items_list'                 => __( 'Categories list', 'text_domain' ),
        'items_list_navigation'      => __( 'Categories list navigation', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true,
        'rewrite'                    => array(
                                        'with_front' => false,
                                        'slug' => 'engineering-blog-category',
                                    ),
    );
    register_taxonomy( 'engineering_taxonomy', array( 'engineering' ), $args );

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

在functions.php 中,我正在创建用户角色并分配自定义角色和功能。看起来像这样:

function userEngineering() {
    add_role('engineer', 'Engineer', array(
        'read' => true,
        'read_post' => true,
        'edit_posts' => true,
        'delete_posts' => true,
    ));
    $role = get_role('contributor');
    $role->add_cap('read_engineering');
    $role->add_cap('edit_engineering');
    $role->add_cap('delete_engineering');
}
add_action('admin_init', 'userEngineering');

当我以管理员身份在仪表板中时,我可以看到帖子类型:

enter image description here

当我在自定义用户角色为“engineering”的账户中时,它消失了:

enter image description here

为什么?帖子类型怎么不见了?

我已经注册并分配了功能。

https://wordpress.stackexchange.com/questions/126096/capability-to-edit-own-posts-and-not-others

我有这个能力(edit_others_posts)

我做错了什么?

0 个答案:

没有答案