【问题标题】:Adding custom capability Wordpress - users can't create new custom post type添加自定义功能 Wordpress - 用户无法创建新的自定义帖子类型
【发布时间】:2016-08-02 15:19:13
【问题描述】:

我的问题是所有功能都按预期工作,但非管理员无法创建新小部件。添加新按钮在那里,但单击时会显示“您没有足够的权限”消息。客户未选中所有标准功能,例如 edit_posts(在我出现之前出于其他原因)。所以也许这就是问题所在?这是我的相关代码:

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'] 我得到 ​​p>

 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”?

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    “您没有足够的权限访问此页面。”消息意味着用户无法访问创建帖子的管理页面 - 在其他功能之前检查,例如“edit_widgets”。例如,在您的情况下,用户具有“edit_widgets”功能,理论上他可以创建新的“小部件”帖子,但他无法访问创建帖子的管理页面。原因如下:

    据我所知,当 register_post_type 的 "show_in_menu" 参数不是 "true" 时,不会创建像 "Add {post_type}" 这样的默认菜单项,因此用户需要内置的 "edit_posts" 功能才能访问“post-new.php?post_type={post_type}”(他们创建新帖子的地方)。

    解决方法:

    1. 将“show_in_menu”设置为 true:将创建默认菜单
    2. 授予“edit_posts”功能(显然,它将允许用户创建共享“ed​​it_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”设置为true,请参阅https://codex.wordpress.org/Function_Reference/register_post_type

    【讨论】:

    • 很高兴知道。如果我记得的话,我最终会提高他们的权限,并且我们都想通了。我认为从长远来看它甚至会更好,所以不太可能改变它,所以我可以测试这个答案。但是,如果我可以进行测试,或者有人回应说这是可行的,我很乐意接受它作为答案。谢谢!
    • 我又在看这个了。 RE 您的解决方法,#1 有效,但我想控制菜单。我为组中的每个 cpt 添加了子菜单项。我的角色确实有edit_widgets。我检查了一下,它是针对角色和当前用户的……另外,我将 map_meta_cap 设置为 true。感谢您的帮助!
    【解决方案2】:

    我想我想通了。我错误地映射了“create_posts”。正如我在问题中提到的,默认的 wp 行为是这样映射的:

    'create_posts' => 'edit_widgets'
    

    创建角色时,我是这样映射的

    'create_posts' => 'create_widgets',
    

    所以我回到默认的映射,它可以工作。

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多