In WordPress, the remove_meta_box
is used to remove metabox from post, page, link or any custom page form in admin section. However, remove_meta_box
function doesn’t work for featured image box (postimagediv). After digging the core function, found a solution here.
I just wanted to remove the ‘postimagediv’ meta box from the post edit form for the ‘contributor’ role. Here’s the code –
add_action( 'admin_menu' , 'remove_post_thumb_meta_box' ); function remove_post_thumb_meta_box() { global $pagenow, $_wp_theme_features; if ( in_array( $pagenow,array('post.php','post-new.php')) && !current_user_can('publish_posts') ) { unset( $_wp_theme_features['post-thumbnails']); } }
How it works
In wp-admin/edit-form-advanced.php
file, they called the postimagediv
meta box in this way –
if ( current_theme_supports('post-thumbnails', $post_type) && post_type_supports( $post_type, 'thumbnail' ) && ( ! is_multisite() || ( ( $mu_media_buttons = get_site_option( 'mu_media_buttons', array() ) ) && ! empty( $mu_media_buttons['image'] ) ) ) ) add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', $post_type, 'side', 'low');
The first logic ( if ( current_theme_supports( 'post-thumbnails', $post_type )))
say, if the current theme && post type doesn’t support post-thumnails
features, featured image meta box will not be placed. So i just removed the post-thumnails
features, when the logged in user can’t publish posts and he is on post.php
or post-new.php
page.
That’s it.