我的問題是,所有功能都按預期工作,除非非管理員無法創建新的小部件。添加新按鈕在那裏,但點擊時顯示'您沒有足夠的權限'消息。客戶端沒有選中edit_posts之類的所有標準功能(出於某種其他原因)。所以也許這是問題?這裏是我的相關代碼:添加自定義功能WordPress的 - 用戶無法創建新的自定義帖子類型
public static function manage_plugin_cap($action = 'add_cap'){
$roles = get_editable_roles();
$caps = array('edit_widget','edit_widgets','read_widgets');
$admin_caps = array('edit_other_widgets','publish_widgets','read_private_widgets','delete_widgets');
foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) {
if (isset($roles[$key])) {
if ($key == 'administrator'){
foreach ($admin_cap as $c){
if ($action=='add_cap')
{$role->add_cap($c, false);}
else {$role->{$action}($c);}
}
}
foreach ($cap as $c){
if ($action=='add_cap')
{$role->add_cap($c, false);}
else {$role->{$action}($c);}
}
}
}
}
register_post_type($this->prefix.'_'.$single, $args);
$args = array(
'labels' => $labels,
'description' => __('Description.', $this->text_domain),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type={$single}',
'query_var' => true,
'rewrite' => array('slug' => $single,'with_front'=>true),
'capability_type' => 'widget',
'has_archive' => $plural,
'can_export' => true,
'hierarchical' => false,
'menu_position' => null
);
有沒有人看到我失蹤?我嘗試添加'create_widgets'沒有成功,'publish_widgets'添加到所有角色(即使我不希望他們能夠),也沒有任何作用。謝謝!
編輯:我從args中刪除'capability_type'。當我定義添加我自己的時候,我不認爲這是必要的(也許它只是在從現有功能進行映射時需要,因此wp知道如何命名新功能)。
更新:我有機會重溫這個嘗試做正確的事。 我有這些元素在我的register_post_type陣列:
'capability_type' => 'widget',
'map_meta_cap' => false,
這似乎爲除create_widgets一切工作。 當我傾倒$ GLOBALS [ 'wp_post_types'] [ 'my_cpt']我得到
public 'cap' =>
object(stdClass)[434]
public 'edit_post' => string 'edit_widget' (length=10)
public 'read_post' => string 'read_widget' (length=10)
public 'delete_post' => string 'delete_widget' (length=12)
public 'edit_posts' => string 'edit_widgets' (length=11)
public 'edit_others_posts' => string 'edit_others_widgets' (length=18)
public 'publish_posts' => string 'publish_widgets' (length=14)
public 'read_private_posts' => string 'read_private_widgets' (length=19)
public 'create_posts' => string 'edit_widgets' (length=11)
所以我不知道爲什麼會出現 'create_posts' 爲 'edit_widgets'?
很高興知道。如果我回憶起來,我結束了他們的許可權限,並且我們完全明白了。我認爲它從長遠來看甚至更好,所以我不可能改變它,所以我可以測試這個答案。但是如果我能夠接受測試,或者任何人迴應說這是有效的,我很樂意接受它作爲答案。謝謝! – user3080408
我正在看這個。注意你的解決方法,#1有效,但我想控制菜單。我爲該組中的每個cpt添加了子菜單項。我的角色確實有edit_widgets。我查了一下,它是爲角色和當前用戶而存在的...另外,我將map_meta_cap設置爲true。謝謝您的幫助! – user3080408