【发布时间】: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