How can I allow the subscriber role to edit and delete their own custom post type

I have a custom post type, and i want a subscriber roll to be able to edit and delete their own posts. i have the next code:

function agents_create_post_type() {
$labels = array(
    'name'               => __( 'Agents', 'ee' ),
    'singular_name'      => __( 'Agent', 'ee' ),
    'add_new'            => __( 'Add New', 'ee' ),
    'add_new_item'       => __( 'Add New Agent', 'ee' ),
    'edit_item'          => __( 'Edit Agent', 'ee' ),
    'new_item'           => __( 'New Agent', 'ee' ),
    'all_items'          => __( 'All Agents', 'ee' ),
    'view_item'          => __( 'View Agent', 'ee' ),
    'search_items'       => __( 'Search Agent', 'ee' ),
    'not_found'          => __( 'No agents found', 'ee' ),
    'not_found_in_trash' => __( 'No agents found in Trash', 'ee' ),
    'parent_item_colon'  => '',
            'menu_name'          => __( 'Agents', 'ee' ),
);
    $capabilities = array(
        'publish_posts' => 'publish_agents',
        'edit_posts' => 'edit_agents',
        'edit_others_posts' => 'edit_others_agents',
        'delete_posts' => 'delete_agents',
        'delete_others_posts' => 'delete_others_agents',
        'read_private_posts' => 'read_private_agents',
        'edit_post' => 'edit_agent',
        'delete_post' => 'delete_agent',
        'read_post' => 'read_agent'
        );
    register_post_type( 'agent',
    array(
        'labels'        => $labels,
        'rewrite'       => array(
        'slug' => __( 'agents', 'ee' )
            ),
        'hierarchical'  => true,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
        'public'        => true,
        'has_archive'   => true,
        'menu_position' => 32,
                    'map_meta_cap'=>true,
                    'capabilities' => $capabilities,
        'menu_icon'     => get_template_directory_uri() .'/img/agents.png',
    )
);


}

add_action( 'init', 'agents_create_post_type' );


function add_capability() {
    $admins = get_role( 'administrator' );
    $admins->add_cap( 'publish_agents' ); 
    $admins->add_cap( 'edit_agents' ); 
    $admins->add_cap( 'edit_others_agents' ); 
    $admins->add_cap( 'delete_agents' ); 
    $admins->add_cap( 'delete_others_agents' ); 
    $admins->add_cap( 'read_private_agents' ); 
    $admins->add_cap( 'edit_agent' ); 
    $admins->add_cap( 'delete_agent' ); 
    $admins->add_cap( 'read_agent' ); 

    $subscriber = get_role( 'subscriber' ); 
    $subscriber->add_cap( 'edit_agents' ); 
    $subscriber->add_cap( 'delete_agents' ); 

}
add_action( 'admin_init', 'add_capability');

With that done I got the menu for Subscriber in the panel control to Add and View Agents, but when I list them, even though I have especified permissions for subscriber it seems once the post have been published by admin the links to edit, delete por each post don’t appear anymore, i thought with the permissions above would be enough. Any clue would be very grateful!

Related posts

Leave a Reply

1 comment

  1. Add this:
    edit_published_agents cap…
    and check this link: https://codex.wordpress.org/Function_Reference/get_post_type_capabilities

    function agents_create_post_type() {
    $labels = array(
        'name'               => __( 'Agentes', 'ee' ),
        'singular_name'      => __( 'Agente', 'ee' ),
        'add_new'            => __( 'Add New', 'ee' ),
        'add_new_item'       => __( 'Add New Agent', 'ee' ),
        'edit_item'          => __( 'Edit Agent', 'ee' ),
        'new_item'           => __( 'New Agent', 'ee' ),
        'all_items'          => __( 'All Agents', 'ee' ),
        'view_item'          => __( 'View Agent', 'ee' ),
        'search_items'       => __( 'Search Agent', 'ee' ),
        'not_found'          => __( 'No agents found', 'ee' ),
        'not_found_in_trash' => __( 'No agents found in Trash', 'ee' ),
        'parent_item_colon'  => '',
        'menu_name'          => __( 'Agentes', 'ee' ),
    );
        $capabilities = array(
            'publish_posts' => 'publish_agents',
            'edit_posts' => 'edit_agents',
            'edit_others_posts' => 'edit_others_agents',
            'delete_posts' => 'delete_agents',
            'delete_others_posts' => 'delete_others_agents',
            'read_private_posts' => 'read_private_agents',
            'edit_post' => 'edit_agent',
            'delete_post' => 'delete_agent',
            'read_post' => 'read_agent',
            'edit_published_posts' => 'edit_published_agents'
            );
        register_post_type( 'agent',
        array(
            'labels'        => $labels,
            'rewrite'       => array(
            'slug' => __( 'agents', 'ee' )
                ),
            'hierarchical'  => true,
            'supports'      => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
            'public'        => true,
            'has_archive'   => true,
            'menu_position' => 32,
                        'map_meta_cap'=>true,
                        'capabilities' => $capabilities,
            'menu_icon'     => get_template_directory_uri() .'/img/agents.png',
        )
    );
    
    
    }
    add_action( 'init', 'agents_create_post_type' );
    
    
    function add_capability() {
    
        // capabilities for administrator
        $admins = get_role( 'administrator' );
        $admins->add_cap( 'publish_agents' );
        $admins->add_cap( 'edit_agents' );
        $admins->add_cap( 'edit_others_agents' );
        $admins->add_cap( 'delete_agents' );
        $admins->add_cap( 'delete_others_agents' );
        $admins->add_cap( 'read_private_agents' );
        $admins->add_cap( 'edit_agent' );
        $admins->add_cap( 'delete_agent' );
        $admins->add_cap( 'read_agent' );
    
        // capabilities for subscriber
        // delete caps that you don´t use
        $subscriber = get_role( 'subscriber' );
        $subscriber->add_cap( 'publish_agents' );
        $subscriber->add_cap( 'edit_agents' );
        $subscriber->add_cap( 'edit_others_agents' );
        $subscriber->add_cap( 'edit_published_agents' );
        $subscriber->add_cap( 'delete_agents' );
        $subscriber->add_cap( 'delete_others_agents' );
        $subscriber->add_cap( 'read_private_agents' );
        $subscriber->add_cap( 'edit_agent' );
        $subscriber->add_cap( 'delete_agent' );
        $subscriber->add_cap( 'read_agent' );
    }
    add_action( 'admin_init', 'add_capability');