每个角色都有能力。
这里有一个简单的贡献者:(他只是写作和编辑)
add_role(\'contributor\', \'Contributor\', array(
\'read\' => true,
\'edit_posts\' => true,
\'delete_posts\' => false,
));
现在他有了新的能力(他可以编辑其他人的帖子)
function add_capability()
{
$role = get_role(\'contributor\');
$role->add_cap(\'edit_others_posts\');
}
add_action(\'admin_init\', \'add_capability\');
现在为我们的新岗位类型添加他的新能力。首先让我们创建帖子类型:
function create_myposttype()
{
register_post_type(
\'myposttype\',
array(
\'public\' => true,
\'capability_type\' => \'myposttype\',
\'capabilities\' => array(
\'publish_posts\' => \'publish_myposttypes\',
\'edit_posts\' => \'edit_myposttypes\',
\'edit_others_posts\' => \'edit_others_myposttypes\',
\'read_private_posts\' => \'read_private_myposttypes\',
\'edit_post\' => \'edit_myposttype\',
\'delete_post\' => \'delete_myposttype\',
\'read_post\' => \'read_myposttype\',
),
)
);
}
add_action(\'init\', \'create_myposttype\');
已创建帖子类型,但投稿人对此没有权限。让我们给他一些果汁:
function add_capability()
{
$role = get_role(\'contributor\');
$role->add_cap(\'read_myposttype\');
$role->add_cap(\'edit_myposttypes\');
$role->add_cap(\'delete_myposttype\', false); // to be sure
}
add_action(\'admin_init\', \'add_capability\');
read\\u post和read\\u*功能确保具有指定角色的用户无法查看和访问当前区域。他会得到一般的Wordpress错误:
您没有足够的权限访问此页面
让我们的作者(默认角色)无法看到myposttype 职位:
function remove_author_capability()
{
$role = get_role(\'author\');
$role->add_cap(\'read_myposttype\', false);
}
add_action(\'admin_init\', \'remove_author_capability\');
请记住清理:
function add_roles_on_activation() // add roles on activation
{
add_role(\'contributor\', \'Contributor\', array(
\'read\' => true,
\'edit_posts\' => true,
\'delete_posts\' => false,
));
}
register_activation_hook(__FILE__, \'add_roles_on_activation\');
function add_roles_removal() // we clean up after ourselves
{
remove_role(\'contributor\');
}
register_deactivatin_hook(__FILE__, \'add_roles_removal\');
Now if you just want to remove stuff from the menu (making them accessible by URL anyway) here is how:
从adminbar(顶部悬停的小菜单):
function my_edit_adminbar($wp_admin_bar)
{
if(current_user_can(\'read_myposttype\'))
$wp_admin_bar->remove_node(\'new-myposttype\');
}
add_action(\'admin_bar_menu\', \'my_edit_adminbar\');
从菜单中:
function my_edit_menu()
{
if(current_user_can(\'read_myposttype\'))
{
$slug = \'edit.php?post_type=myposttype\';
remove_menu_page($slug);
}
}
add_action(\'admin_menu\', \'my_edit_menu\');