WordPress:自定义帖子类型的功能

WordPress:自定义帖子类型的功能,wordpress,custom-post-type,capability,Wordpress,Custom Post Type,Capability,我正在写一个插件,创建一个自定义的post_类型。我还希望插件创建一个自定义角色,它只能添加/编辑/删除新的post_类型。我尝试了几个插件(角色范围器、高级访问管理器),它们允许我重新定义或创建新角色,但不允许我为新的post_类型分配特定的功能。例如,我想允许添加/编辑我的新帖子类型,但不允许添加/编辑普通帖子/页面 根据我所读到的,我可以使用函数添加新角色。此函数的一个参数是一组“功能”,似乎是要定义的。我想我需要的是能够增加我的能力,这是我的职位类型所特有的。这可能吗?自定义帖子类型的功

我正在写一个插件,创建一个自定义的post_类型。我还希望插件创建一个自定义角色,它只能添加/编辑/删除新的post_类型。我尝试了几个插件(角色范围器、高级访问管理器),它们允许我重新定义或创建新角色,但不允许我为新的post_类型分配特定的功能。例如,我想允许添加/编辑我的新帖子类型,但不允许添加/编辑普通帖子/页面

根据我所读到的,我可以使用函数添加新角色。此函数的一个参数是一组“功能”,似乎是要定义的。我想我需要的是能够增加我的能力,这是我的职位类型所特有的。这可能吗?

自定义帖子类型的功能 函数将
$capabilities
数组作为其(可选)参数之一

可能看起来是这样的:

$capabilities = array(
    'publish_posts' => 'publish_ypts',
    'edit_posts' => 'edit_ypts',
    'edit_others_posts' => 'edit_others_ypts',
    'delete_posts' => 'delete_ypts',
    'delete_others_posts' => 'delete_others_ypts',
    'read_private_posts' => 'read_private_ypts',
    'edit_post' => 'edit_ypt',
    'delete_post' => 'delete_ypt',
    'read_post' => 'read_ypt'
);
其中“ypt”代表“您的帖子类型”

此后,您可以向WordPress添加一个新角色,该角色具有这些确切的功能(可能还有一些标准WordPress功能):

后者可以使用插件来完成,例如,查看JustinTadlock的插件

彻底的例子 给你一个更具体的例子:

/* REGISTER POST TYPE */

add_action('init', 'ypt_register');

function ypt_register()
{

    $labels = array(
        'name' => _x('YPTs', 'post type general name'),
        'singular_name' => _x('YPT', 'post type singular name'),
        'add_new' => _x('Add New YPT', 'Team item'),
        'add_new_item' => __('Add a new post of type YPT'),
        'edit_item' => __('Edit YPT'),
        'new_item' => __('New YPT'),
        'view_item' => __('View YPT'),
        'search_items' => __('Search YPTs'),
        'not_found' =>  __('No YPTs found'),
        'not_found_in_trash' => __('No YPTs currently trashed'),
        'parent_item_colon' => ''
    );

    $capabilities = array(
        // this is where the first code block from above goes
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'ypt',
        'capabilities' => $capabilities,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title', 'author', 'thumbnail' )
    ); 

    register_post_type( 'ypt' , $args );

    flush_rewrite_rules( false );
}


/* MAP META CAPABILITIES */

add_filter( 'map_meta_cap', 'ypt_map_meta_cap', 10, 4 );

function ypt_map_meta_cap( $caps, $cap, $user_id, $args )
{

    if ( 'edit_ypt' == $cap || 'delete_ypt' == $cap || 'read_ypt' == $cap ) {
        $post = get_post( $args[0] );
        $post_type = get_post_type_object( $post->post_type );
        $caps = array();
    }

    if ( 'edit_ypt' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_posts;
        else
            $caps[] = $post_type->cap->edit_others_posts;
    }

    elseif ( 'delete_ypt' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->delete_posts;
        else
            $caps[] = $post_type->cap->delete_others_posts;
    }

    elseif ( 'read_ypt' == $cap ) {
        if ( 'private' != $post->post_status )
            $caps[] = 'read';
        elseif ( $user_id == $post->post_author )
            $caps[] = 'read';
        else
            $caps[] = $post_type->cap->read_private_posts;
    }

    return $caps;
}
如今(WP 3.5+),这要容易得多。注册帖子类型时,只需将
map\u meta\u cap
参数设置为
TRUE
,并为
capability\u type
参数选择一个
字符串
(通常是帖子类型名称)

一个简单的
var_转储($GLOBALS['wp_post_types']['new_custom_post_types'])将显示如下内容

[cap] => stdClass Object
(
    [edit_post]      => "edit_{$capability_type}"
    [read_post]      => "read_{$capability_type}"
    [delete_post]        => "delete_{$capability_type}"
    [edit_posts]         => "edit_{$capability_type}s"
    [edit_others_posts]  => "edit_others_{$capability_type}s"
    [publish_posts]      => "publish_{$capability_type}s"
    [read_private_posts]     => "read_private_{$capability_type}s"
        [delete_posts]           => "delete_{$capability_type}s"
        [delete_private_posts]   => "delete_private_{$capability_type}s"
        [delete_published_posts] => "delete_published_{$capability_type}s"
        [delete_others_posts]    => "delete_others_{$capability_type}s"
        [edit_private_posts]     => "edit_private_{$capability_type}s"
        [edit_published_posts]   => "edit_published_{$capability_type}s"
)

更为理想的阵列部分是其他七种基本功能,它们不是由core检查的,而是在post类型注册期间由
map\u meta\u caps()
映射的。

如何定义publish_-ypt或任何其他post_类型特定功能?@Emerson我回答的第一部分就是这个定义。
$capabilities
数组是放入
register\u post\u type()
函数的参数之一。在它中,您可以将新功能映射到常规帖子的等效功能。如果在没有此可选参数的情况下使用
register\u post\u type()
,则常规功能适用。如果使用它,pubish_posts功能不包括您的帖子类型-作者必须具有publish_-ypt功能。哦,我明白了。我感到很困惑,因为代码中add_role()的第三个参数叫做$capabilities。所以我在第二个函数中使用了这个数组。我会修复它的报告回来。@请参阅我的上述编辑或检查您的粘贴箱。请注意,由于您显然需要家长和孩子,
“层次结构”
需要设置为true。否则,我的示例也适用于您的字典条目。我编辑了代码,但我发现唯一缺少的是:“功能类型”=>“字典条目”,它仍然不起作用,但我认为我肯定还缺少其他内容。对于仍在学习的noobs,将map_meta_cap设置为true,将capability_type设置为内容类型,将自动为您生成功能,因此您无需指定它们。
[cap] => stdClass Object
(
    [edit_post]      => "edit_{$capability_type}"
    [read_post]      => "read_{$capability_type}"
    [delete_post]        => "delete_{$capability_type}"
    [edit_posts]         => "edit_{$capability_type}s"
    [edit_others_posts]  => "edit_others_{$capability_type}s"
    [publish_posts]      => "publish_{$capability_type}s"
    [read_private_posts]     => "read_private_{$capability_type}s"
        [delete_posts]           => "delete_{$capability_type}s"
        [delete_private_posts]   => "delete_private_{$capability_type}s"
        [delete_published_posts] => "delete_published_{$capability_type}s"
        [delete_others_posts]    => "delete_others_{$capability_type}s"
        [edit_private_posts]     => "edit_private_{$capability_type}s"
        [edit_published_posts]   => "edit_published_{$capability_type}s"
)