I'm trying to conditionally remove the 'Page Attributes' meta box for a specific page (or multiple specific pages). For example:
function remove_meta_boxes() {
if (is_page('contact')) {
remove_meta_box('pageparentdiv', 'page', 'side');
}
}
add_action( 'admin_menu', 'remove_meta_boxes' );
I'm able to remove the Page Attributes meta box for all pages without the conditional statement, so that part is working, but adding the is_page()
condition seems to cause the remove_meta_box()
code not to work. Any help is greatly appreciated.
Thanks!
I'm trying to conditionally remove the 'Page Attributes' meta box for a specific page (or multiple specific pages). For example:
function remove_meta_boxes() {
if (is_page('contact')) {
remove_meta_box('pageparentdiv', 'page', 'side');
}
}
add_action( 'admin_menu', 'remove_meta_boxes' );
I'm able to remove the Page Attributes meta box for all pages without the conditional statement, so that part is working, but adding the is_page()
condition seems to cause the remove_meta_box()
code not to work. Any help is greatly appreciated.
Thanks!
Share Improve this question asked Jul 16, 2019 at 21:22 Sean VinciSean Vinci 314 bronze badges1 Answer
Reset to default 1The is_page()
conditional relies on the global $wp_query
WP_Query object which isn't set on the edit post page. We have some other options though...
If we know the page ID we can test against $_GET:
/**
* Remove metaboxes
*
* @return void
*/
function wpse343020_remove_meta_boxes() {
if( isset( $_GET, $_GET['post'] ) && 123 == $_GET['post'] ) {
remove_meta_box( 'pageparentdiv', 'page', 'side' );
}
}
add_action( 'admin_menu', 'wpse343020_remove_meta_boxes' );
If you really need to test against the page title or page slug you can hook in later whenever the global $post
WP_Post object is available:
/**
* Remove metaboxes
*
* @return void
*/
function wpse343020_remove_meta_boxes() {
global $post;
if( ! empty( $post ) && 'contact' == $post->post_name ) {
remove_meta_box( 'pageparentdiv', 'page', 'side' );
}
}
add_action( 'admin_head', 'wpse343020_remove_meta_boxes' );