我创建了一个名为Event
并且我在我的functions.php
文件,但我的自定义用户角色仍然无法看到事件编辑页面上的特色图像元框。如果用户以管理员身份登录Featured Image
元框显示无任何错误。
以下是我注册CPT的代码:
add_action(\'init\', \'event_post_init\');
function event_post_init() {
// array of all capabilities for our CPT
$capabilities = array(
\'publish_posts\' => \'publish_events\',
\'edit_posts\' => \'edit_events\',
\'edit_others_posts\' => \'edit_others_events\',
\'delete_posts\' => \'delete_events\',
\'delete_published_posts\' => \'delete_published_events\',
\'delete_others_posts\' => \'delete_others_events\',
\'read_private_posts\' => \'read_private_events\',
\'edit_post\' => \'edit_event\',
\'delete_post\' => \'delete_event\',
\'read_post\' => \'read_event\',
);
// register the CPT
register_post_type( \'event\',
array(
\'labels\' => array(
\'name\' => __(\'Event\')
),
\'public\' => true,
\'has_archive\' => true,
\'show_ui\' => true,
\'menu_position\' => 8,
\'capability_type\' => array(\'event\', \'events\'),
\'capabilities\' => $capabilities,
\'supports\' => array(\'title\', \'thumbnail\', \'page-attributes\'),
\'map_meta_cap\' => true,
\'hierarchical\' => true,
)
);
}
下面是我如何设置自定义用户角色:
function create_event_admin_role(){
add_role(\'event_admin\', \'Event Administrator\', array(
\'publish_events\' => false,
\'edit_events\' => true,
\'edit_others_events\' => false,
\'delete_events\' => false,
\'delete_others_events\' => false,
\'read_private_events\' => true,
\'edit_published_events\' => true,
\'read\' => true,
\'assign_terms\' => true,
\'edit_terms\' => true,
\'manage_terms\' => true,
\'read_private_pages\' => true
)
);
}
奇怪的是,如果我查看编辑页面的标记,我可以看到
#postimagediv
元素,但由于某些原因它被隐藏。以下是页面中的标记:
<div class="postbox hide-if-js" id="postimagediv" style="">
<div title="Click to toggle" class="handlediv"><br></div>
<h3 class="hndle ui-sortable-handle"><span>Featured Image</span></h3>
<div class="inside">
<p class="hide-if-no-js">
<a class="thickbox" id="set-post-thumbnail" href="http://example.com/wp-admin/media-upload.php?post_id=662&type=image&TB_iframe=1&width=753&height=294" title="Set featured image">Set featured image</a>
</p>
</div>
</div>
而实际上隐藏元框的css:
#postimagediv {
display: hidden !important;
}
请注意,我
have 已启用
Featured Image
在下面
Screen Options
.
也许我还应该指出,我已经尝试过赋予上述角色upload_files
使用以下代码的权限:
function extend_event_admin_role() {
$role = get_role(\'event_admin\');
$role->add_cap(\'upload_files\');
}
另一件需要指出的是,如果我
do 添加
upload_files
许可,那么
Featured Image
在下可见
Screen Options
还有其他支持媒体的元框
Add Media
按钮,如果我设置
upload_files
到
false
消失。
如果我在中更改代码
wp-admin/edit-form-advanced.php
它添加了特色图像元盒,然后我可以看出它真的在呼叫
add_metabox()
:
if ( $thumbnail_support && current_user_can( \'upload_files\' ) ):
add_meta_box(\'postimagediv\', __(\'Featured Image\'), \'post_thumbnail_meta_box\', null, \'side\', \'low\');
print \'Support for Featured Image\';
exit;
endif;
SO网友:Webloper
据我所知,您需要自定义帖子类型Event
+ featured_image
+ Custom Role
, 我不了解的是capability\\u类型的事件和事件,除非并且直到您要定制很多,否则这种定制的原因是什么?
我在这里所做的是克隆编辑器角色event_amdin
使用它的所有功能,并在问题中使用页面属性时将capability\\u type设置为页面。
测试:创建了两个用户,分别作为参与者和事件管理员,其工作正常。请提供有关您需要的更多信息,以便我更新此答案。谢谢
Edit 1 : 更新代码,选中“编辑1开始/结束部分到”cloneUserRole
作用
快乐的编码!!
function cloneUserRole()
{
global $wp_roles;
if (!isset($wp_roles))
$wp_roles = new WP_Roles();
$editor = $wp_roles->get_role(\'editor\');
// Adding a new role with all editor caps.
$wp_roles->add_role(\'event_admin\', \'Event Administrator\', $editor->capabilities);
// edit 1 start : updated to add cap to new user role
$event_admin = $wp_roles->get_role(\'event_admin\');
$event_admin->add_cap( \'read_event\' );
$event_admin->add_cap( \'delete_event\' );
$event_admin->add_cap( \'edit_event\' );
$event_admin->add_cap( \'read_private_events\' );
$event_admin->add_cap( \'delete_others_events\' ); // don\'t add
$event_admin->add_cap( \'delete_published_events\' );
$event_admin->add_cap( \'delete_events\' ); // don\'t add
$event_admin->add_cap( \'edit_others_events\' ); // don\'t add
$event_admin->add_cap( \'edit_events\' );
$event_admin->add_cap( \'publish_events\' ); // don\'t add
// edit 1 ends :
//echo \'<pre>\', print_r( $wp_roles, 1), \'</pre>\';
//die;
}
add_action(\'init\', \'cloneUserRole\');
function stack_event_init() {
$labels = array(
\'name\' => _x( \'Events\', \'post type general name\', \'stack\' ),
\'singular_name\' => _x( \'Event\', \'post type singular name\', \'stack\' ),
\'menu_name\' => _x( \'Events\', \'admin menu\', \'stack\' ),
\'name_admin_bar\' => _x( \'Event\', \'add new on admin bar\', \'stack\' ),
\'add_new\' => _x( \'Add New\', \'event\', \'stack\' ),
\'add_new_item\' => __( \'Add New Event\', \'stack\' ),
\'new_item\' => __( \'New Event\', \'stack\' ),
\'edit_item\' => __( \'Edit Event\', \'stack\' ),
\'view_item\' => __( \'View Event\', \'stack\' ),
\'all_items\' => __( \'All Events\', \'stack\' ),
\'search_items\' => __( \'Search Events\', \'stack\' ),
\'parent_item_colon\' => __( \'Parent Events:\', \'stack\' ),
\'not_found\' => __( \'No events found.\', \'stack\' ),
\'not_found_in_trash\' => __( \'No events found in Trash.\', \'stack\' )
);
$args = array(
\'labels\' => $labels,
\'description\' => __( \'Description.\', \'stack\' ),
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_nav_menus\' => true,
\'show_in_menu\' => true,
\'show_in_admin_bar\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'event\' ),
\'capability_type\' => \'page\',
\'has_archive\' => true,
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'thumbnail\', \'page-attributes\' )
);
register_post_type( \'event\', $args );
}
add_action(\'init\', \'stack_event_init\');
function my_rewrite_flush() {
stack_event_init();
flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'my_rewrite_flush\' );
- Roles and Capabilities
- Register post type
- Flush Rewrite Rules