Wordpress自定义帖子类型角色

时间:2017-05-17 16:26:38

标签: wordpress custom-post-type

我在wordpress中创建了一个自定义帖子类型,作为管理员我可以用它做任何事情。但我想创建一个角色"客户"只能添加,编辑和删除此自定义帖子类型的内容,因此他可能不会发布常规帖子,编辑或删除它以及页面相同的内容。

这些是我的自定义帖子类型的参数

$args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'publicly_queryable' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array(
        'title',
        'thumbnail'
    ),
    'capabilities' => array(
        'edit_post' => 'edit_portfolio',
        'edit_posts' => 'edit_portfolios',
        'edit_others_posts' => 'edit_others_portfolios',
        'publish_posts' => 'publish_portfolios',
        'read_post' => 'read_portfolios',
        'read_private_posts' => 'read_private_portfolios',
        'delete_post' => 'delete_portfolio',
    ),
    'map_meta_cap' => true,
    'menu_icon'   => 'dashicons-screenoptions',
    'menu_position' => 5,
    'show_ui' => true,
    'exclude_from_search' => false
);

这些是我创建的角色

add_role("client", "Client", array(
    'read' => true, // allows this capability, dashboard
    'upload_files'=>true, //allows user to upload files
    'edit_posts' => false, // denies  user to edit their own posts
    'edit_pages' => false, // denies  user to edit pages
    'edit_others_posts' => false, // denies  user to edit others posts not just their own
    'create_posts' => false, // denies  user to create new posts
    'manage_categories' => false, // denies user to manage post categories
    'publish_posts' => false, // denies the user to publish
    'edit_themes' => false, // false denies this capability. User can’t edit your theme
    'install_plugins' => false, // User cant add new plugins
    'update_plugin' => false, // User can’t update any plugins
    'update_core' => false, // user cant perform core updates
    'edit_portfolios' => true, // allows editing of the user’s own portfolio
    'edit_others_portfolios' => true, // allows the user to edit everyone else’s portfolio
    'delete_portfolios' => true, // allows to delete portfolio written by that user
    'delete_others_portfolios' => true, // allows to delete portfolio written by other users
    'publish_portfolios' => true // allows the user to publish portfolio, otherwise posts stays in draft mode
));

现在的效果是,他们可以看到信息中心中的帖子,但不能更改或点击它,无法删除,当他们发布消息时,他们最终会在错误屏幕上显示他们不应该看到这个。< / p>

提前致谢

2 个答案:

答案 0 :(得分:0)

我从来没有以编程方式完成此操作,但如果你不介意使用插件,我在几个网站上使用了这个https://wordpress.org/plugins/user-roles-and-capabilities/并快速简便。

据我所知,有些人对插件有厌恶,但我对此没有任何问题。

答案 1 :(得分:0)

我找到了答案,所以对于那些也有这个问题的人。 您必须放置此功能并将其设置为true

'delete_posts'=>'delete_portfolios',
'delete_others_posts'=>'delete_others_portfolios',
'delete_published_posts'=>'delete_published_portfolios',
'delete_private_posts'=>'delete_private_portfolios'