Skip to content

Instantly share code, notes, and snippets.

@natebot
Last active September 21, 2023 17:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natebot/6235883 to your computer and use it in GitHub Desktop.
Save natebot/6235883 to your computer and use it in GitHub Desktop.
Test if a WordPress meta box has been registered.
<?php
/**
* Check if a meta box has been registered.
* Returns true if the supplied meta box id has been registered. false if not.
* Note that this does not check what page type
* @param String $meta_box_id An id for the meta box.
* @param String $post_type The post type the box is registered to (optional)
* @return mixed Returns boolean or a WP_Error if $wp_meta_boxes is not yet set.
*/
function is_meta_box_registered( $meta_box_id, $post_type = false ){
global $wp_meta_boxes, $grist_meta_box_found;
$grist_meta_box_found = false; // assume not found by default
// if meta boxes are not yet set up, let's issue an error
if( empty( $wp_meta_boxes ) || ! is_array( $wp_meta_boxes ) )
return new WP_Error( 'missing-meta-boxes', 'global $wp_meta_boxes is not the expected array' );
// should we only look at meta boxes for a specific post type?
if ( $post_type )
$meta_boxes = $wp_meta_boxes[ $post_type ];
else
$meta_boxes = $wp_meta_boxes;
// step through each meta box registration and check if the supplied id exists
array_walk_recursive( $meta_boxes, function( $value, $key, $meta ){
global $grist_meta_box_found;
if ( $key === 'id' && strtolower( $value ) === strtolower( $meta ) )
$grist_meta_box_found = true;
}, $meta_box_id );
$return = $grist_meta_box_found; // temp store the return value
unset( $grist_meta_box_found ); // remove var from from global space
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment