Back to Community

Troubleshooting CMB2's 'show_on' and 'show_on_cb' Parameters for Conditional Metabox Display

22 threads Sep 10, 2025 PluginCmb2

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_on parameter, 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_cb callback 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