2016-08-02 111 views
0

我的問題是,所有功能都按預期工作,除非非管理員無法創建新的小部件。添加新按鈕在那裏,但點擊時顯示'您沒有足夠的權限'消息。客戶端沒有選中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'?

回答

1

「您沒有足夠的權限訪問此頁面。」消息意味着用戶無法訪問創建帖子的管理頁面 - 在其他功能(如「edit_widgets」)之前檢查該頁面。例如,在你的情況下,用戶具有「edit_widgets」功能,理論上他可以創建新的「widget」帖子,但他無法訪問創建帖子的管理頁面。這是爲什麼:

據我所知,當register_post_type的「show_in_menu」參數是「true」以外的任何值時,不會創建像「添加{post_type}」這樣的默認菜單項,因此用戶需要內置「edit_posts」能夠訪問「post-new.php?post_type = {post_type}」(他們在哪裏創建新帖子)。

解決方法:

  1. 集 「show_in_menu」 爲true:默認的菜單將被創建
  2. 授予 「edit_posts」 的能力(很明顯,它將允許用戶創建共享 「edit_posts」 能力的職位)。
  3. 手動添加子菜單並將「edit_widgets」設置爲訪問頁面所需的功能(如果此菜單頁嵌套在第二級菜單項下,則它將不可見 - 如果您將多個帖子類型分組到一個菜單父)

    add_submenu_page({parent_menu_link}, 'Add Widget', 'Add Widget', 'edit_widgets', 'post-new.php?post_type={post_type}');

這裏的關鍵是「edit_widgets」的能力,這將使「添加小組件」可訪問頁面的人誰擁有「edit_widgets」的能力。

PS:您可能還需要設置「map_meta_cap」爲真,看到https://codex.wordpress.org/Function_Reference/register_post_type

+0

很高興知道。如果我回憶起來,我結束了他們的許可權限,並且我們完全明白了。我認爲它從長遠來看甚至更好,所以我不可能改變它,所以我可以測試這個答案。但是如果我能夠接受測試,或者任何人迴應說這是有效的,我很樂意接受它作爲答案。謝謝! – user3080408

+0

我正在看這個。注意你的解決方法,#1有效,但我想控制菜單。我爲該組中的每個cpt添加了子菜單項。我的角色確實有edit_widgets。我查了一下,它是爲角色和當前用戶而存在的...另外,我將map_meta_cap設置爲true。謝謝您的幫助! – user3080408

0

我想我想通這一個out.I是映射「create_posts」不正確。正如我在我的問題提到的,默認的wp行爲是映射這樣的:

'create_posts' => 'edit_widgets' 

創建的角色的時候,我就是這樣

'create_posts' => 'create_widgets', 

測繪於是我又回到了映射,如默認和有用。

相關問題