WordPress

php – WordPress: Rola użytkownika niestandardowego nie ma dostępu do niestandardowego typu wpisu | „Przepraszamy, nie masz uprawnień dostępu do tej strony”

  • 21 lipca, 2021
  • 4 min read
php – WordPress: Rola użytkownika niestandardowego nie ma dostępu do niestandardowego typu wpisu | „Przepraszamy, nie masz uprawnień dostępu do tej strony”


Po zakończeniu rejestracji niestandardowego wpisu użyj poniższego kodu, który będzie Ci służył jako punkt odniesienia.

/**
 * 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;

}


Źródło

Warto przeczytać!  Jak zduplikować bazę danych WordPress za pomocą phpMyAdmin