WordPress:自定义用户角色无法访问自定义帖子类型|“对不起,您不能访问此页面”

时间:2021-07-21 作者:Scott White

The Objective:创建自定义帖子类型,并仅授予管理员和自定义角色查看/控制它的权限。

The Problem:对于管理员来说,它工作得非常好,但对于我得到的自定义角色:Sorry, you are not allowed to access this page.

起初,我认为这可能只是访问它的能力问题,但这段代码却有所不同:

add_submenu_page( /*  STAFF PAGES   */
                \'redacted\', //Parent Menu Slug
                \'Staff Pages\', //Page Title text
                \'Staff Pages\', //Menu Title text
                \'edit_staff\', //Capability required for this menu to be displayed by user
                \'edit.php?post_type=staff\' //Link to page
);
自定义角色可以看到指向自定义帖子类型的链接,但无法访问该链接。还有,跑步print_r($wp_roles->get_role( \'supervisor\' )->capabilities); 确实表明该角色正确地拥有必要的能力。关于如何解决这个问题,我已经有了一些理论,但到目前为止,还没有一个得到证实。

我的代码如下:

function initialize_plugin(){
//Non-relevant code redacted
add_action( \'admin_init\', array($this, \'admin_init\') );
}
function activate(){
    $this->custom_post_types();
    $this->adjust_user_roles();
    //Non-relevant code redacted
}



/* My Custom Post Type */
function custom_post_types(){
            register_post_type( \'staff\', array(
                \'labels\' => array(
                    //labels redacted
                ),
                \'has_archive\'       => false,
                \'hierarchical\'      => true,
                \'menu_icon\'         => \'dashicons-groups\',
                \'capability_type\'   => array(\'staff\', \'staffs\'),
                \'map_meta_cap\'      => true,
                \'public\'            => true,
                \'show_in_menu\'      => false,
                \'rewrite\'           => array( \'slug\' => \'staff\', \'with_front\' => false ),
                \'supports\'          => array( \'title\', \'thumbnail\', \'custom-fields\', \'revisions\'),
                \'show_in_rest\'      => true,
                \'taxonomies\'        => array( \'member-type\' ),
                \'menu_position\'     => 2,
            ));



/* My Custom Role */
function adjust_user_roles(){
$wp_roles = new WP_Roles(); 

$wp_roles->add_role(
              \'supervisor\', __( \'Supervisor\' ),
               array(
                    //General
                    \'moderate_comments\'         => true,
                    \'upload_files\'              => true,
                   
                    //Blog Posts
                    \'read\'                      => true,
                    \'read_post\'                 => true,
                    \'edit_post\'                 => true,
                    \'edit_posts\'                => true,
                    \'edit_others_posts\'         => true,
                    \'delete_posts\'              => false, //Can\'t delete posts

                    //Staff (Custom Post Type)
                    \'create_staffs\'             => true,
                    \'read_staff\'                => true,
                    \'edit_staff\'                => true,
                    \'edit_staffs\'               => true,
                    \'edit_others_staffs\'        => true,
                    \'edit_published_staffs\'     => true,
                    \'edit_private_staffs\'       => true,
                    \'delete_staff\'              => true,
                    \'delete_others_staffs\'      => true,
                    \'delete_published_staffs\'   => true,
                    \'delete_private_staffs\'     => true,
                    \'publish_staffs\'            => true,
                    \'read_private_staffs\'       => true,
              )
);



/* Adding to administrator */
function admin_init(){
   //Non-relevant code redacted
   $this->adjust_user_capabilities("add");
}

function adjust_user_capabilities($action, $roles=array(\'administrator\',\'editor\', \'supervisor\')){
  $staffCaps = array(
                \'create_staff\',
                \'read_staff\',
                \'edit_staff\',
                \'edit_staffs\',
                \'edit_others_staffs\',
                \'edit_published_staffs\',
                \'edit_private_staffs\',
                \'delete_staff\',
                \'delete_others_staffs\',
                \'delete_published_staffs\',
                \'delete_private_staffs\',
                \'publish_staffs\',
                \'read_private_staffs\',              
            );

            //Cycle through each role
            foreach($roles as $roleType) :
                $role = get_role( $roleType );
            
                //Add each capability
                if($action == "add"){
                    foreach($staffCaps as $staffCap){   
                        $role->add_cap( $staffCap );
                    }
                }
            
                //Remove each capability
                elseif($action == "remove"){
                    foreach($staffCaps as $staffCap){
                        $role->remove_cap( $staffCap );
                    }
                }
            endforeach;
}
NOTE:此代码出现在wp-content/plugins/myplugin/myplugin.php. 此外,为了清晰起见,我编辑了代码中一些不相关的部分,例如添加或删除子菜单,并试图阐述更多的结构。如果我遗漏了什么或有人有任何疑问,请随时告诉我-D

In Closing:我可能只是一个忽视了一些显而易见的事情的大白痴,但无论如何,我非常感谢所有的帮助/建议/建议!如果我自己能得到答案,我会把它加入到这次讨论中,以帮助其他人解决类似的问题和/或我未来的自我lol

2 个回复
最合适的回答,由SO网友:Scott White 整理而成

SOLUTION:玩了一会儿,我意识到我绝对是个白痴,而且想得太多了。而我之前在this similar post, 我最终用他们的代码代替了我的代码,发现它实际上适用于我的用例。在试图理解原因的过程中,我开始尝试将其转换为我的,并很快找到了问题的根源:

/* My Custom Post Type */
function custom_post_types(){
            register_post_type( \'staff\', array(
                \'labels\' => array(
                    //labels redacted
                ),
                \'has_archive\'       => false,
                \'hierarchical\'      => true,
                \'menu_icon\'         => \'dashicons-groups\',
                \'capability_type\'   => array(\'staff\', \'staffs\'),
                \'map_meta_cap\'      => true,
                \'public\'            => true,
/*---------> */ \'show_in_menu\'      => false, /* <---------*/
                \'rewrite\'           => array( \'slug\' => \'staff\', \'with_front\' => false ),
                \'supports\'          => array( \'title\', \'thumbnail\', \'custom-fields\', \'revisions\'),
                \'show_in_rest\'      => true,
                \'taxonomies\'        => array( \'member-type\' ),
                \'menu_position\'     => 2,
            ));
为了有一个干净的自定义菜单,我设置show_in_menu 为我制造了问题。当我把它改成\'show_in_menu\' => true, 我的问题解决了。在解决这个问题时,我很想尝试一下remove_menu_page(); 或者考虑一些更优雅的东西。

无论如何,今天的教训是不要过分专注于一个方面。希望这有助于其他人和快乐的编码!

SO网友:Anjan

在您自定义后注册完成后,请使用下面的类型代码,它将帮助您作为参考。

/**
 * Post Type: Blogs.
 */
function cptui_register_blog_cpts() {


    $labels = [
        "name" => __( "Blogs", "oba" ),
        "singular_name" => __( "Blog", "oba" ),
        "menu_name" => __( "Blogs", "oba" ),
        "all_items" => __( "All Blogs", "oba" ),
        "add_new" => __( "Add Blog", "oba" ),
        "add_new_item" => __( "Add New Blog", "oba" ),
        "edit_item" => __( "Edit Blog", "oba" ),
        "new_item" => __( "New Blog", "oba" ),
        "view_item" => __( "View Blog", "oba" ),
        "view_items" => __( "View Blog", "oba" ),
        "search_items" => __( "Search Blogs", "oba" ),
        "not_found" => __( "No Blogs Found", "oba" ),
        "not_found_in_trash" => __( "No Blogs found in Trash", "oba" ),
        "parent" => __( "Parent Blog", "oba" ),
        "featured_image" => __( "Featured image for this Blog", "oba" ),
        "set_featured_image" => __( "Set Featured image for this Blog", "oba" ),
        "remove_featured_image" => __( "Remove featured Image for this Blog", "oba" ),
        "use_featured_image" => __( "Use as featured image for this Blog", "oba" ),
        "archives" => __( "Blogs Archive", "oba" ),
        "insert_into_item" => __( "Insert into Blog", "oba" ),
        "uploaded_to_this_item" => __( "Uploaded to this Blog", "oba" ),
        "filter_items_list" => __( "Filter Blogs List", "oba" ),
        "items_list_navigation" => __( "Blog List Navigation", "oba" ),
        "items_list" => __( "Blogs list", "oba" ),
        "attributes" => __( "Blogs Attributes", "oba" ),
        "name_admin_bar" => __( "Blog", "oba" ),
        "item_published" => __( "Blog Published", "oba" ),
        "item_published_privately" => __( "Blog Published privately", "oba" ),
        "item_reverted_to_draft" => __( "Blog reverted to draft", "oba" ),
        "item_scheduled" => __( "Blog scheduled", "oba" ),
        "item_updated" => __( "Blog updated", "oba" ),
        "parent_item_colon" => __( "Parent Blog", "oba" ),
    ];

    $args = [
        "label" => __( "Blogs", "oba" ),
        "labels" => $labels,
        "description" => "This is a post type of Blog reading page",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "blog",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => [ "slug" => "blog", "with_front" => true ],
        "query_var" => true,
        "supports" => [ "title", "editor", "thumbnail", "custom-fields", "comments", "revisions", "author"],
        "taxonomies" => [ "blog_category", "blog_post_tag", "blog_post_author_name" ],
        // "capabilities" => array( 
        //         "manage_terms" => "manage_categories", 
        //         "edit_terms" => "manage_categories", 
        //         "delete_terms" => "manage_categories", 
        //         "assign_terms" => "edit_posts" 
        //     ), 
    ];

    register_post_type( "blog", $args );
    register_taxonomy(\'blog_category\', \'blog\', array(\'hierarchical\' => true, \'label\' => \'Blog Category\', \'query_var\' => true, \'rewrite\' => array( \'slug\' => \'blog-category\' )));
    // register_taxonomy(\'blog_post_author_name\', \'blog\', array(\'hierarchical\' => true, \'label\' => \'E-Books Author\', \'query_var\' => true, \'rewrite\' => array( \'slug\' => \'blog-post-author-name\' )));
}

add_action( \'init\', \'cptui_register_blog_cpts\' );

/**
 ** add teachers capability
 */
add_action(\'admin_init\',\'blog_add_role_caps\',999);
    function blog_add_role_caps() {

        // Add the roles you\'d like to administer the custom post types
        $roles = \'administrator\';

        // Loop through each role and assign capabilities
        // foreach($roles as $the_role) {    
             // $role = get_role($the_role);               
             $role = get_role($roles);               
             $role->add_cap( \'read\' );
             $role->add_cap( \'read_blog\');
             $role->add_cap( \'edit_blog\' );
             $role->add_cap( \'edit_blogs\' );
             $role->add_cap( \'edit_published_blogs\' );
             $role->add_cap( \'publish_blogs\' );
             $role->add_cap( \'delete_published_blogs\' );
        // }
        }
        /**
 * Overwrite args of custom post type registered by plugin
 */
add_filter( \'register_post_type_args\', \'change_capabilities_of_blog\' , 10, 2 );

function change_capabilities_of_blog( $args, $post_type ){

 // Do not filter any other post type
 if ( \'blog\' !== $post_type ) {

     // Give other post_types their original arguments
     return $args;

 }

 // Change the capabilities of the "book" post_type
 $args[\'capabilities\'] = array(
            \'edit_post\' => \'edit_blog\',
            \'edit_posts\' => \'edit_blogs\',
            \'edit_others_posts\' => \'edit_other_blogs\',
            \'publish_posts\' => \'publish_blogs\',
            \'read_post\' => \'read_blog\',
            \'read_private_posts\' => \'read_private_blogs\',
            \'delete_post\' => \'delete_blog\',
        );

  // Give the course_document post type it\'s arguments
  return $args;

}

相关推荐

如何在unction.php中更改图片url?

例如,我的图像url是https://aa.com/wp-content/uploads/2021/07/x-600x600.jpg我想把它改成https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg或https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg.web