Troubleshooting Common CMB2 Metabox Display Issues: Why Your Box Isn't Showing
Content
If you've integrated the powerful CMB2 library into your WordPress site to create custom metaboxes, only to find they're not appearing where you expect, you're not alone. This is one of the most common hurdles developers face. Based on community discussions and solutions, let's explore why this happens and how to fix it.
Why Your CMB2 Metabox Might Not Appear
Several configuration issues can prevent a metabox from displaying. The good news is that most are simple to diagnose and resolve.
1. Incorrect `object_types` Parameter
The most frequent cause is an incorrect object_types parameter when initializing your metabox. This parameter tells CMB2 which WordPress editor screens should display the box.
Problem Example: A user reported their basic metabox code wasn't working. The issue was often traced back to this parameter not matching the intended post type, page, or screen.
Solution: Double-check that the 'object_types' array in your new_cmb2_box() call contains the correct values. For a post, it's 'post'. For a page, it's 'page'. For a user profile, it's 'user'. For a custom post type, use its exact slug.
// Correctly defining object_types for a page
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page' ), // This MUST be correct
// ... other parameters
) );
2. Conditional Display with `show_on_cb`
Sometimes, you only want a metabox to appear under specific conditions, like for a particular page or user role. Using the show_on_cb (show on callback) parameter is the correct way to handle this.
Problem Example: A developer tried to disable CMB2 on the front-end by removing its init action with is_single(), which is not the intended method and won't work.
Solution: Use the 'show_on_cb' parameter to provide a callback function that returns true (show the box) or false (hide the box). This is the method recommended by the CMB2 team.
// Using a callback to conditionally show a metabox
$cmb = new_cmb2_box( array(
'id' => 'conditional_metabox',
'title' => __( 'Conditional Metabox', 'cmb2' ),
'object_types' => array( 'page' ),
'show_on_cb' => 'my_show_on_callback_function', // Your custom function
) );
// Define the callback function
function my_show_on_callback_function( $cmb ) {
// Your logic here. Return true to show, false to hide.
return get_the_ID() === 42; // Example: Only show on page ID 42
}
The CMB2 Snippet Library has numerous examples of conditional display logic you can adapt.
3. Hook Priority and Execution Order
WordPress hooks execute in a specific order. If your code runs before the post type is registered or before a parent theme's CMB2 code, it can cause problems.
Problem Example: A user with a child theme couldn't see their metabox because the parent theme's code, which defined the metabox they were trying to modify, hadn't run yet.
Solution: Ensure your CMB2 code runs at the appropriate time. The standard hook is cmb2_admin_init. If you need to remove or modify a metabox created by another plugin or theme, you may need to use a later priority (a higher number) on the same hook to ensure your code runs *after* the original metabox is created.
// Run your function later to ensure another metabox exists first
add_action( 'cmb2_admin_init', 'my_modification_function', 20 ); // Higher priority
Summary and Next Steps
If your CMB2 metabox is missing, walk through this checklist:
- Verify
object_types: Is it set to the correct post type, user, or option page? - Check for conditional callbacks: Are you using
show_on_cb? If so, test the logic in your callback function to ensure it returnstruewhen expected. - Review hook priority: If modifying existing boxes, is your code running after the original is set up?
For further reading, the official CMB2 example functions file is an invaluable resource packed with commented examples of proper configuration.
By methodically checking these common areas, you can usually pinpoint and resolve the issue, getting your custom metaboxes to display perfectly.
Related Support Threads Support
-
Meta Box on WordPress Admin Dashboardhttps://wordpress.org/support/topic/meta-box-on-wordpress-admin-dashboard/
-
CMB2_Boxes::add( $cmb ) not workinghttps://wordpress.org/support/topic/cmb2_boxesadd-cmb-not-working/
-
How to display Category Metabox fields in frontend ?https://wordpress.org/support/topic/how-to-display-category-metabox-fields-in-frontend/
-
How can I make a menu show in Admin and Editor?https://wordpress.org/support/topic/how-can-i-make-a-menu-show-in-admin-and-editor/
-
CMB2 PHP class?https://wordpress.org/support/topic/cmb2-php-class/
-
Trying to get cmb_styles options workinghttps://wordpress.org/support/topic/trying-to-get-cmb_styles-options-working/
-
Hooking CMB2 into WooCommerce Product Uploadshttps://wordpress.org/support/topic/hooking-cmb2-into-woocommerce-product-uploads/
-
Modify the existing Featured Image box with CMB2https://wordpress.org/support/topic/modify-the-existing-featured-image-box-with-cmb2/
-
Any plans for Gutenberg’s removal of PHP metaboxeshttps://wordpress.org/support/topic/any-plans-for-gutenbergs-removal-of-php-metaboxes/
-
Read-only Field for Specific User Rolehttps://wordpress.org/support/topic/read-only-field-for-specific-user-role/
-
Show and hide metaboxhttps://wordpress.org/support/topic/show-and-hide-metabox/
-
CMB2 – how to add metabox inside an existing options page ?https://wordpress.org/support/topic/cmb2-how-to-add-metabox-to-an-existing-options-page/
-
CMB2 Rest API Questionhttps://wordpress.org/support/topic/cmb2-rest-api-question/
-
Accessing CMB2 data structures inside filtershttps://wordpress.org/support/topic/accessing-cmb2-data-structures-inside-filters/
-
Hide menu linkhttps://wordpress.org/support/topic/hide-menu-link/
-
Is CMB2 for my use?https://wordpress.org/support/topic/is-cmb2-for-my-use/
-
Metabox configuration is required to have an ID parameterhttps://wordpress.org/support/topic/metabox-configuration-is-required-to-have-an-id-parameter/
-
Add meta boxes to specific page/post?https://wordpress.org/support/topic/add-meta-boxes-to-specific-pagepost/
-
General setup questionhttps://wordpress.org/support/topic/general-setup-question/
-
Something stupidhttps://wordpress.org/support/topic/something-stupid/
-
Box orderhttps://wordpress.org/support/topic/box-order/
-
Can't add user metaboxes to an MU site?https://wordpress.org/support/topic/cant-add-user-metaboxes-to-an-mu-site/
-
remove default field from themehttps://wordpress.org/support/topic/remove-default-field-from-theme/
-
Disable CMB2 on some viewhttps://wordpress.org/support/topic/disable-cmb2-on-some-view/
-
Adding Custom HTML to Metaboxhttps://wordpress.org/support/topic/adding-custom-html-to-metabox/
-
cmb2 and wp-jsonhttps://wordpress.org/support/topic/cmb2-and-wp-json/
-
CMB2 meta fields and core/editor WP Redux storehttps://wordpress.org/support/topic/cmb2-meta-fields-and-core-editor-wp-redux-store/
-
CMB2 Options not appearing in admin sidebarhttps://wordpress.org/support/topic/cmb2-options-not-appearing-in-admin-sidebar/
-
Conditional Metabox Displayhttps://wordpress.org/support/topic/conditional-metabox-display/
-
How can I link CMB2 to a Custom Page?https://wordpress.org/support/topic/how-can-i-link-cmb2-to-a-custom-page/
-
CMB2 For Grouped Productshttps://wordpress.org/support/topic/cmb2-for-grouped-products/
-
New ‘after_title’ and ‘after_editor’ contexts push the Publish metabox downhttps://wordpress.org/support/topic/new-after_title-and-after_editor-contexts-push-the-publish-metabox-down/
-
WP 5.5 and Alternative Contextshttps://wordpress.org/support/topic/wp-5-5-and-alternative-contexts/
-
CMB2 field conditional logichttps://wordpress.org/support/topic/cmb2-field-conditional-logic/
-
Parent theme uses CMB, can I alter settings for child theme?https://wordpress.org/support/topic/parent-theme-uses-cmb-can-i-alter-settings-for-child-theme/
-
Displaying Admin Options for other users besides admin?https://wordpress.org/support/topic/displaying-admin-options-for-other-users-besides-admin/
-
How tohttps://wordpress.org/support/topic/how-to-88/
-
cmb2_override_meta_value filter not run on WooCommerce Edit Order page (HPOS)https://wordpress.org/support/topic/cmb2_override_meta_value-filter-not-run-on-woocommerce-edit-order-page-hpos/
-
Best way to add form to existing admin page?https://wordpress.org/support/topic/best-way-to-add-form-to-existing-admin-page/
-
Not possible to have multiple fields in a metabox when one of them is not used?https://wordpress.org/support/topic/not-possible-to-have-multiple-fields-in-a-metabox-when-one-of-them-is-not-used/
-
Simple/r way to save fieldgroup form in a different, non-cmb2 admin metabox?https://wordpress.org/support/topic/simple-r-way-to-save-fieldgroup-form-in-a-different-non-cmb2-admin-metabox/