如何允许订阅者角色编辑和删除自己的自定义帖子类型

时间:2014-04-08 15:22:48

标签: wordpress custom-post-type capability

我有一个自定义帖子类型,我希望订阅者滚​​动能够编辑和删除自己的帖子。我有下一个代码:

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');

完成后,我在面板控件中将订阅者的菜单添加到添加和查看代理,但是当我列出它们时,即使我已经为订阅者指定了权限,似乎一旦该帖子由管理员发布了编辑链接,删除每个帖子不再出现,我认为上面的权限就足够了。任何线索都会非常感激!

1 个答案:

答案 0 :(得分:-1)

添加:     edit_published_agents上限...     并查看此链接: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');