Using WordPress ‘get_hidden_meta_boxes()’ PHP function

The get_hidden_meta_boxes() WordPress PHP function retrieves an array of IDs of hidden meta boxes for a specific screen.

Usage

get_hidden_meta_boxes( $screen );

Custom Example:

// Input
$hidden_meta_boxes = get_hidden_meta_boxes( 'post' );

// Output
Array ( [0] => 'custom_meta_box_id' )

Parameters

  • $screen (string|WP_Screen) (Required) – The screen identifier.

More information

See WordPress Developer Resources: get_hidden_meta_boxes()

Examples

Display Hidden Meta Boxes for a Post Screen

Retrieve and display hidden meta boxes for the ‘post’ screen.

function display_hidden_post_meta_boxes() {
  $hidden_meta_boxes = get_hidden_meta_boxes( 'post' );
  echo 'Hidden meta boxes for post screen: <br />';
  foreach ( $hidden_meta_boxes as $meta_box ) {
    echo $meta_box . '<br />';
  }
}

Check if a Meta Box is Hidden

Check if a specific meta box is hidden on the ‘page’ screen.

function is_meta_box_hidden( $meta_box_id ) {
  $hidden_meta_boxes = get_hidden_meta_boxes( 'page' );
  return in_array( $meta_box_id, $hidden_meta_boxes );
}

Hide a Meta Box

Hide a meta box by adding it to the list of hidden meta boxes for a custom post type screen.

function hide_meta_box( $meta_box_id, $screen ) {
  $hidden_meta_boxes = get_hidden_meta_boxes( $screen );
  $hidden_meta_boxes[] = $meta_box_id;
  update_user_option( get_current_user_id(), "meta-box-hidden_$screen", $hidden_meta_boxes );
}

Show a Meta Box

Show a hidden meta box by removing it from the list of hidden meta boxes for a custom post type screen.

function show_meta_box( $meta_box_id, $screen ) {
  $hidden_meta_boxes = get_hidden_meta_boxes( $screen );
  $key = array_search( $meta_box_id, $hidden_meta_boxes );
  if ( false !== $key ) {
    unset( $hidden_meta_boxes[$key] );
  }
  update_user_option( get_current_user_id(), "meta-box-hidden_$screen", $hidden_meta_boxes );
}

Toggle Meta Box Visibility

Toggle the visibility of a meta box on the ‘post’ screen.

function toggle_meta_box_visibility( $meta_box_id ) {
  $hidden_meta_boxes = get_hidden_meta_boxes( 'post' );
  $key = array_search( $meta_box_id, $hidden_meta_boxes );
  if ( false !== $key ) {
    unset( $hidden_meta_boxes[$key] );
  } else {
    $hidden_meta_boxes[] = $meta_box_id;
  }
  update_user_option( get_current_user_id(), "meta-box-hidden_post", $hidden_meta_boxes );
}