Troubleshooting CMB2's 'show_on' and 'show_on_cb' Parameters for Conditional Metabox Display
Content
One of the most powerful features of CMB2 is the ability to conditionally display metaboxes on specific pages, posts, or templates. However, configuring the show_on and show_on_cb parameters can be a common source of confusion, as seen in numerous community discussions. This guide will walk you through the most frequent issues and their solutions.
The Common Problem: Metaboxes Not Appearing Where Expected
Users often report that their carefully configured metabox does not appear on the intended admin screens. The root cause typically falls into one of these categories:
- Incorrect syntax for the
show_onparameter, especially when using arrays. - Attempting to use front-end conditional tags (like
is_home()) within a callback, which do not work in the WordPress admin. - A misunderstanding of how to structure a custom callback function for
show_on_cb. - Expecting the metabox to dynamically appear after changing a page template without saving and refreshing the page.
Solution 1: Correctly Using the 'show_on' Parameter
The show_on parameter is excellent for simple conditions based on a page template or post ID. A frequent mistake is nesting arrays incorrectly when specifying multiple templates.
Incorrect Syntax (from Thread 1):
'show_on' => array( 'key' => 'page-template', array( 'value' => array( 'template-menu.php', 'template-about.php' ) ) ),
Correct Syntax (from Thread 1):
'show_on' => array( 'key' => 'page-template', 'value' => array( 'template-menu.php', 'template-about.php' ) ),
This correct structure tells CMB2 to show the metabox on pages using either the 'template-menu.php' or 'template-about.php' template.
Solution 2: Mastering the 'show_on_cb' Callback
For more complex logic, such as checking if a page is the static front page or combining multiple conditions, the show_on_cb parameter is your best tool. You provide a function that returns true (show the box) or false (hide the box).
Example: Show only on the static front page (from Thread 7):
function my_show_on_static_front_page() {
global $post;
// Check if the current post's ID matches the front page setting
return ( $post->ID == get_option( 'page_on_front' ) );
}
// In your metabox array:
'show_on_cb' => 'my_show_on_static_front_page',
Important Note: As highlighted in Thread 7, the show_on_cb parameter works on individual fields and the main metabox definition (new_cmb2_box), but it may not work as expected for group fields (add_group_field). For group fields, conditional logic often needs to be handled within the group's field definitions or with custom JavaScript.
Solution 3: Understanding the Limits of Dynamic Display
A question from Thread 15 asks if metaboxes can appear dynamically after a user changes a page template without refreshing. The CMB2 library does not include JavaScript to handle this out-of-the-box. The conditional checks (show_on, show_on_cb) run once when the page is loaded. To achieve a dynamic interface, a custom JavaScript solution would be required to show/hide metaboxes based on user interaction, which is an advanced implementation.
Best Practices and Final Tips
- Use 'show_on_cb' for Complex Logic: If your conditions extend beyond a simple template or ID check, a callback function offers the most flexibility and is often recommended over
show_on(as suggested in Thread 16). - Check Your Screen: When writing a custom callback for an options page, ensure you are correctly checking the screen ID, as demonstrated in Thread 14.
- Performance: If you have a very complex metabox that loads slow data, consider using a
show_on_cbcallback to prevent it from loading on every admin screen. This can help mitigate performance issues mentioned in Thread 12.
By understanding the nuances of these two parameters, you can precisely control your metabox display and build more intuitive editing experiences.
Related Support Threads Support
-
show_on not working for multiple page templateshttps://wordpress.org/support/topic/show_on-not-working-for-multiple-page-templates/
-
Callback Function on CMB2https://wordpress.org/support/topic/callback-function-on-cmb2/
-
How to display custom post type instances by id/slug with shortcode?https://wordpress.org/support/topic/how-to-display-custom-post-type-instances-by-idslug-with-shortcode/
-
show_on – exclude on templatehttps://wordpress.org/support/topic/show_on-exclude-on-template/
-
Doesn’t working in Archive Pages: Shop, Bloghttps://wordpress.org/support/topic/doesnt-working-in-archive-pages-shop-blog/
-
Help with object_types => options-pagehttps://wordpress.org/support/topic/help-with-object_types-options-page/
-
How to extend show_onhttps://wordpress.org/support/topic/how-to-extend-show_on/
-
Show shortcodes in wysiwyg cmb2https://wordpress.org/support/topic/show-shortcodes-in-wysiwyg-cmb2/
-
using multiple cmb2 blockshttps://wordpress.org/support/topic/using-multiple-cmb2-blocks/
-
Display only on pages and not on articleshttps://wordpress.org/support/topic/display-only-on-pages-and-not-on-articles/
-
Help with custom show_onhttps://wordpress.org/support/topic/help-with-custom-show_on/
-
What to use instead of current_screen to prevent loading?https://wordpress.org/support/topic/what-to-use-instead-of-current_screen-to-prevent-loading/
-
I’m overlooking something when trying to use the Show On Filtershttps://wordpress.org/support/topic/im-overlooking-something-when-trying-to-use-the-show-on-filters/
-
CMB2 – place inside options page created by ‘add_options_page’https://wordpress.org/support/topic/cmb2-place-inside-options-page-creaded-by-add_options_page/
-
Is it possible to use show_on filters without refreshing the page?https://wordpress.org/support/topic/is-it-possible-to-use-show_on-filters-without-refreshing-the-page/
-
Using ‘show_on’ with an array of page templates, can’t get it to work…https://wordpress.org/support/topic/using-show_on-with-an-array-of-page-templates-cant-get-it-to-work/
-
Use “show_on” with multiple post types and different templateshttps://wordpress.org/support/topic/use-show_on-with-multiple-post-types-and-different-templates/
-
Object type & show onhttps://wordpress.org/support/topic/object-type-show-on/
-
show on page using specific page templatehttps://wordpress.org/support/topic/show-on-page-using-specific-page-template/
-
Is Template-specific metabox creation possible?https://wordpress.org/support/topic/is-template-specific-metabox-creation-possible/
-
custom show_on filter not workinghttps://wordpress.org/support/topic/custom-show_on-filter-not-working/
-
Can I use CMB2’s show_on_cb within another Plugin instead of functions.php?https://wordpress.org/support/topic/can-i-use-cmb2s-show_on_cb-within-another-plugin-instead-of-functions-php/