检查以下代码-
add_action( \'admin_init\', \'the_dramatist_add_role_caps\', 999 );
function the_dramatist_add_role_caps() {
// Add the roles you\'d like to administer the custom post types
$roles = array( \'editor\', \'administrator\' );
// Loop through each role and assign capabilities
foreach($roles as $the_role) {
$role = get_role($the_role);
$role->add_cap( \'read\' );
$role->add_cap( \'read_{Post_Type_A_Name}\' ); // Also do for Post_Type_B
$role->add_cap( \'read_private_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'edit_{Post_Type_A_Name}\' ); // Also do for Post_Type_B
$role->add_cap( \'edit_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'edit_others_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'edit_published_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'publish_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'delete_others_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'delete_private_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
$role->add_cap( \'delete_published_{Post_Type_A_Name}s\' ); // Also do for Post_Type_B
}
}
通过这种方式,您可以向任何用户角色添加权限。
如果要为应获得权限的用户创建新角色,请创建如下新角色-
function add_the_dramatist_role() {
add_role(\'the_dramatist_role\',
\'The Dramatist Role\',
array(
\'read\' => true,
\'edit_posts\' => false,
\'delete_posts\' => false,
\'publish_posts\' => false,
\'upload_files\' => true,
)
);
}
// Remember this must be called plugin main file.
register_activation_hook( __FILE__, \'add_the_dramatist_role\' );
然后添加
the_dramatist_role
到上一个函数
the_dramatist_add_role_caps
\'s
$roles
像这样的变量-
$roles = array( \'the_dramatist_role\', \'editor\', \'administrator\' );
现在
the_dramatist_role
将拥有所有这些特权。
希望以上帮助。