custom post type capabilities

Support MB Custom Post Type custom post type capabilitiesResolved

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #11464
    brkardbrkard
    Participant

    Hi.

    I need to add some custom capabilities to cpts.

    Plugin is only permits post,page cababilites.

    For example i want to add movies cpt and give capability type movies.

    How can achive this ?

    Thanks

    #11480
    Anh TranAnh Tran
    Keymaster

    Hi Burak,

    Currently, it's not supported yet. Let me try to figure a way to do it inside the plugin.

    #11486
    brkardbrkard
    Participant

    Hi Anh.

    It will be good feature if you can figure a way to do inside the plugin.

    Thank you very much.

    #13549
    meta_geckometa_gecko
    Participant

    Any updates on this request? Unfortunately need to create own types for now because of the capabilities limitation..

    #13579
    Anh TranAnh Tran
    Keymaster

    I've just updated the plugin today, adding a "Custom" rule for capability type. With that, the plugin auto generate capabilities for the new post type like: edit_cpt, delete_cpt. Please try and let me know how it goes.

    #13761
    meta_geckometa_gecko
    Participant

    Hi Anh,

    Thanks for this, however it doesn't appear to be complete. The custom capability is added, however the capabilities do not appear to be mapped to the core caps required by administrators (or any one else who is granted the capability) to edit published posts etc.

    There should be at least 11 capabilities for the admin role.

    *edit. adding all of the capabilities might not be necessary since they should be inherited.

    It seems that when registering the CPT using MB the mapping of the capabilities are missing:

    'map_meta_cap' => true,

    #13763
    meta_geckometa_gecko
    Participant

    Just to confirm, adding 'map_meta_cap' => true, solves the problem. Doing so maps all of the primitive capabilities to the custom capability.

    #13769
    Anh TranAnh Tran
    Keymaster

    Hi Guy,

    I've added that attribute. But in my test, even when adding it, the post type doesn't show in the admin menu for admins to view/edit them.

    #13782
    meta_geckometa_gecko
    Participant

    Hi Anh,

    It works fine, but when you add a custom capability then that capability needs to be given to any roles who need it, including administrators.

    This isn't the responsibility of Meta Box, it's up to the admin to provide the appropriate permissions to any roles that need it.

    The only thing I'd suggest is that you might want to add some information to the documentation because many people won't know this otherwise.

    #13792
    Anh TranAnh Tran
    Keymaster

    It works fine, but when you add a custom capability then that capability needs to be given to any roles who need it, including administrators.
    This isn’t the responsibility of Meta Box, it’s up to the admin to provide the appropriate permissions to any roles that need it.

    That's correct. I think a plugin like Members would do the job better than Meta Box, since it's too verbose for capability management.

    I'll add the info to the docs when updating the plugin.

    #36161
    Alaan TVAlaan TV
    Participant

    I tried to set 'capability_type' => 'custom' in my custom post type, but I was not able to see the custom capabilities in my Roles and Capabilities plugin.

    Then, I tried to keep the custom option and assigned the custom capabilities (that should be generated by the MB plugin) to the Administrator role by code, but I couldn't see the custom post type menu. Like this:

    $role = get_role( 'administrator' );
    
    $role->add_cap( 'edit_news_post',        true );
    $role->add_cap( 'read_news_post',        true );
    $role->add_cap( 'delete_news_post',      true );
    $role->add_cap( 'edit_news',             true );
    $role->add_cap( 'edit_others_news',      true );
    $role->add_cap( 'publish_news',          true );
    $role->add_cap( 'read_private_news',     true );
    $role->add_cap( 'read',                  true );
    $role->add_cap( 'delete_news',           true );
    $role->add_cap( 'delete_private_news',   true );
    $role->add_cap( 'delete_published_news', true );
    $role->add_cap( 'delete_others_news',    true );
    $role->add_cap( 'edit_private_news',     true );
    $role->add_cap( 'edit_published_news',   true );
    $role->add_cap( 'edit_news',             true );
    

    Lastly, I tried to access the URL of the post type manually: /wp-admin/edit.php?post_type=news, but I got the following error message:

    You need a higher level of permission.
    Sorry, you are not allowed to edit posts in this post type.

    Here's the code I used to create my custom post type:

    function register_post_type__news()
    {
        $labels = [
            'name'                     => esc_html('News'),
            'singular_name'            => esc_html('News Post'),
            'add_new'                  => esc_html('Add New'),
            'add_new_item'             => esc_html('Add new News post'),
            'edit_item'                => esc_html('Edit News Post'),
            'new_item'                 => esc_html('New News Post'),
            'view_item'                => esc_html('View News Post'),
            'view_items'               => esc_html('View News'),
            'search_items'             => esc_html('Search News'),
            'not_found'                => esc_html('No News found'),
            'not_found_in_trash'       => esc_html('No News found in Trash'),
            'parent_item_colon'        => esc_html('Parent News Post:'),
            'all_items'                => esc_html('News'),
            'archives'                 => esc_html('News Post Archives'),
            'attributes'               => esc_html('News Post Attributes'),
            'insert_into_item'         => esc_html('Insert into News Post'),
            'uploaded_to_this_item'    => esc_html('Uploaded to this News Post'),
            'featured_image'           => esc_html('Featured image'),
            'set_featured_image'       => esc_html('Set featured image'),
            'remove_featured_image'    => esc_html('Remove featured image'),
            'use_featured_image'       => esc_html('Use as featured image'),
            'menu_name'                => esc_html('News'),
            'filter_items_list'        => esc_html('Filter News Post list'),
            'filter_by_date'           => esc_html(''),
            'items_list_navigation'    => esc_html('News list navigation'),
            'items_list'               => esc_html('News list'),
            'item_published'           => esc_html('News Post published'),
            'item_published_privately' => esc_html('News Post published privately'),
            'item_reverted_to_draft'   => esc_html('News Post reverted to draft'),
            'item_scheduled'           => esc_html('News Post scheduled'),
            'item_updated'             => esc_html('News Post updated'),
            'text_domain'              => esc_html('post-type-news'),
        ];
        $args = [
            'label'               => esc_html('News'),
            'labels'              => $labels,
            'description'         => 'The main post type that is used to create and publish news articles.',
            'public'              => true,
            'hierarchical'        => false,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'show_ui'             => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'show_in_rest'        => true,
            'query_var'           => true,
            'can_export'          => true,
            'delete_with_user'    => false,
            'has_archive'         => true,
            'rest_base'           => '',
            'show_in_menu'        => 'edit.php',
            'position'            => 1,
            'menu_icon'           => 'dashicons-admin-site-alt',
            'capability_type'     => 'custom',
            'supports'            => ['title', 'editor', 'thumbnail', 'excerpt', 'author', 'revisions', 'post-formats', 'sticky', 'featured-avatar'],
            'taxonomies'          => ['category', 'post_tag', 'topics'],
            'rewrite'             => [
                'with_front' => false,
            ],
        ];
    
        register_post_type( 'news', $args );
    }
    

    Could you please advise me?

    #38497
    Olivier van HeldenOlivier van Helden
    Participant

    I was looking for the same info and after digging, I found out that caps added must match capability_type value ('custom' above).

    So if you set 'capability_type' => 'custom', the code should be:

    
    $role->add_cap( 'delete_customs',        true );
    $role->add_cap( 'edit_customs',        true );
    $role->add_cap( 'edit_others_customs',        true );
    $role->add_cap( 'publish_customs',        true );
    $role->add_cap( 'read_private_customs',        true );
    // ...
    

    (note cap type is singular and caps added are plural)

    But if you set `'capability_type' => 'news_post', then you can use:

    
    $role->add_cap( 'delete_news_posts',        true );
    $role->add_cap( 'edit_news_posts',        true );
    $role->add_cap( 'edit_others_news_posts',        true );
    $role->add_cap( 'publish_news_posts',        true );
    $role->add_cap( 'read_private_news_posts',        true );
    
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.