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

标签 wordpress custom-post-type capability

我正在编写一个创建自定义 post_type 的插件。我还希望该插件创建一个自定义角色,该角色只能添加/编辑/删除新的 post_type。我已经尝试了几个插件(Role Scoper、Advanced Access manager),它们允许我重新定义或创建新角色,但它们不允许我分配特定于新 post_type 的功能。例如,我希望允许添加/编辑我的新 post_type,但不允许添加/编辑普通帖子/页面。

根据我所读到的内容,我可以使用 add_role() 添加新角色功能。该函数的参数之一是一组“功能”,它似乎被定义为 here 。我认为我需要的是能够添加特定于 MY post_type 的功能。这可能吗?

最佳答案

自定义帖子类型的功能

函数register_post_type()$capability 数组作为其(可选)参数之一。

它可能看起来像这样:

$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 功能)的新角色:

add_role(
    'ypt_author',
    'Author of your post type',
    array(
        'publish_ypts' => true,
        'edit_ypts' => true,
        'edit_others_ypts' => true,
        'delete_ypts' => true,
        'delete_others_ypts' => true,
        'read_private_ypts' => true,
        'edit_ypt' => true,
        'delete_ypt' => true,
        'read_ypt' => true,
        // more standard capabilities here
    )
);

后者可以使用插件来完成,请查看 Members例如,Justin Tadlock 的插件。

完整的示例

给你一个更具体的例子:

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

关于WordPress:自定义帖子类型的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8198038/

相关文章:

WordPress 自定义帖子类型 每月存档

jquery - 添加 jQuery 脚本到 WordPress 管理

java - VSTS 构建代理 : Java 9

Reactjs wordpress 为 SEO 预渲染现有客户端应用程序

wordpress - 在 WordPress 中创建隐藏的自定义帖子类型

css - WordPress 网站页面和帖子(左侧和右侧)的填充调整

r - 在 R 中访问控制图结果?

javascript - 跨域访问 iframe 元素

javascript - iframe通信中的post消息有键值系统吗

php - WordPress 图片路径正确,但网站上不显示图片