1

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!

1

The 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' );
  • Thanks for your solutions! I was able to get the first one to work, using the page ID, but not the second one, using the slug. And unfortunately I do need to use the slug because sometimes I'm dealing with slightly different instances of the database depending on the environment and so the page IDs aren't always the same. Any idea why using the slug didn't work? – Sean Vinci Jul 24 at 20:33
  • Maybe you can print the $post and $_REQUEST to see if the slug exists in either of those: printf( '<pre>%s</pre>', print_r( $post, 1 ) ); die( 'end' ); – Howdy_McGee Jul 24 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.