最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - Remove meta box for specific page

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 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' );
发布评论

评论列表(0)

  1. 暂无评论