admin管理员组文章数量:1429560
I am building a website that has a bunch of pages and two post pages:
- News
- What's New
Both of these pages will be like category pages. How can I allow one set of users to only create content for the What's New section?
For example:
- One set of users can only create content for What's New
- One set of users can only create content for News
How can I enforce a rule like this?
I am building a website that has a bunch of pages and two post pages:
- News
- What's New
Both of these pages will be like category pages. How can I allow one set of users to only create content for the What's New section?
For example:
- One set of users can only create content for What's New
- One set of users can only create content for News
How can I enforce a rule like this?
Share Improve this question asked May 3, 2019 at 18:19 vegemite4mevegemite4me 1353 bronze badges1 Answer
Reset to default 1The simplest way is to create two custom post types. One will be "news" and one will be "whats-new".
So for example, you could create a plugin in /wp-content/plugins/
called news-post-types.php
. The code below will create special permissions that only apply to this post type:
<?php
/* Plugin Name: Post types and permissions for News and What's New
*/
// on init, create a custom post type called News
add_action('init', 'wpse_create_post_types');
function wpse_create_post_types() {
// Capabilities: this is how you'll enable some users to edit, delete, etc.
$capabilities = array(
'edit_post' => 'edit_news',
'read_post' => 'read_news',
'delete_post' => 'delete_news',
'create_posts' => 'create_news',
'delete_posts' => 'delete_news',
'delete_others_posts' => 'delete_others_news',
'delete_private_posts' => 'delete_private_news',
'delete_published_posts' => 'delete_published_news',
'edit_posts' => 'edit_news',
'edit_others_posts' => 'edit_others_news',
'edit_private_posts' => 'edit_private_news',
'edit_published_posts' => 'edit_published_news',
'publish_posts' => 'publish_news',
'read_private_posts' => 'read_private_news'
);
// Other CPT arguments
$args = array(
// Important: make sure to include the next 2 args for capabilities
'map_meta_cap' => true,
'capabilities' => $capabilities,
// You can adjust the other args as needed
'public' => true,
'has_archive' => true,
'hierarchical' => false,
'supports' => array('title', 'editor', 'author', 'page-attributes')
);
// Actually create the post type
register_post_type('news', $args);
}
As long as you specify has_archive
as true when you register the custom post type, you will also automatically get a category-like page for each one, without having to create any custom templates.
Make sure to activate your plugin and also visit the Settings > Permalinks page. You don't need to change anything there, but this will flush your rewrite rules so the new URLs will worok.
So now, you have a custom post type with custom capabilities. You need to do at least one more thing: create a role that has these permissions. Right now, none of your users (even Admins) have access - that's because you have just created custom capabilities that aren't automatically mapped to any users.
In the same plugin file:
// 'news_author' is a slug, 'News Author' is how you will see this role in wp-admin
add_role('news_author', 'News Author', array(
// You may or may not want to give them all these capabilities.
// For example you could let them publish but not delete.
'delete_news' => true,
'create_news' => true,
'delete_news' => true,
'delete_others_news' => true,
'delete_private_news' => true,
'delete_published_news' => true,
'edit_news' => true,
'edit_others_news' => true,
'edit_private_news' => true,
'edit_published_news' => true,
'publish_news' => true,
'read_private_news' => true,
// If you want them to be able to upload files and read on the front end,
// make sure to include the following 2 capabilities:
'read' => true,
'upload_files' => true
));
Finally, you may also want to have Admins (like yourself perhaps) be able to add/edit/delete these custom News posts. If so, add this to the plugin:
add_action('admin_init', 'wpse_add_admin_caps');
function wpse_add_admin_caps() {
$role = get_role('administrator');
$role->add_cap('delete_news');
$role->add_cap('create_news');
$role->add_cap('delete_news');
$role->add_cap('delete_others_news');
$role->add_cap('delete_private_news');
$role->add_cap('delete_published_news');
$role->add_cap('edit_news');
$role->add_cap('edit_others_news');
$role->add_cap('edit_private_news');
$role->add_cap('edit_published_news');
$role->add_cap('publish_news');
$role->add_cap('read_private_news');
}
You can do the same thing for your "What's New" section - create a second custom post type, then add a role for it, then optionally add those capabilities to the administrator role as well.
本文标签: taxonomyDifferent permissions for same type of post
版权声明:本文标题:taxonomy - Different permissions for same type of post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745526409a2661854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论