Skip to content
Justin Sternberg edited this page Aug 24, 2019 · 21 revisions

Table of Contents generated with DocToc


Metaboxes should be context-specific, and only show up when relevant. There's two tools you can use to limit their display:

Both of these are used when you define your metabox.

Limit to specific post types

For every metabox you create, you should specify the post types to which it applies. They'll be listed as an array. Here's an example that only applies to pages:

$cmb = new_cmb2_box( array(
	'id'           => 'test_metabox',
	'title'        => 'Test Metabox',
	'object_types' => array( 'page' ), // post type
	'context'      => 'normal', //  'normal', 'advanced', or 'side'
	'priority'     => 'high',  //  'high', 'core', 'default' or 'low'
	'show_names'   => true, // Show field names on the left
) );

Here's an example that displays on posts and events:

$cmb = new_cmb2_box( array(
	'id'           => 'test_metabox',
	'title'        => 'Test Metabox',
	'object_types' => array( 'post', 'event' ), // post type
	'context'      => 'normal', //  'normal', 'advanced', or 'side'
	'priority'     => 'high',  //  'high', 'core', 'default' or 'low'
	'show_names'   => true, // Show field names on the left
) );

Limit to specific IDs

Let's say you have two pages, About Us (page ID - 50) and Contact Us (page ID - 24). You created a Contact Information metabox that you want only displaying on these two pages. Here's what the beginning of your metabox might look like.

$cmb = new_cmb2_box( array(
	'id'           => 'contact-information',
	'title'        => 'Contact Information',
	'object_types' => array( 'page' ), // post type
	'show_on'      => array( 'key' => 'id', 'value' => array( 50, 24 ) ),
	'context'      => 'normal', //  'normal', 'advanced', or 'side'
	'priority'     => 'high',  //  'high', 'core', 'default' or 'low'
	'show_names'   => true, // Show field names on the left
) );

The type of show_on filter (key) is "id" and the value for that filter is an array of your IDs. If you only wanted it on the About page you could use 'value' => 50 instead of putting it in an array.

Limit to specific page templates

This will limit it to the page template with the file name template-contact.php. If you want to include it on multiple page templates, put them all in an array like in the above example.

$cmb = new_cmb2_box( array(
	'id'           => 'contact-information',
	'title'        => 'Contact Information',
	'object_types' => array( 'page' ), // post type
	'show_on'      => array( 'key' => 'page-template', 'value' => 'template-contact.php' ),
	'context'      => 'normal', //  'normal', 'advanced', or 'side'
	'priority'     => 'high',  //  'high', 'core', 'default' or 'low'
	'show_names'   => true, // Show field names on the left
) );

More Show On Filters

You can also Add your own show_on filters, and that page lists some examples.

Documentation:

  1. Home

  2. Installation
    Installation instructions for various environments

  3. Basic Usage
    Get up and running

  4. Advanced Usage
    Not for the faint of heart

  5. Field Types
    Breakdown of field types

  6. Examples
    Examples for registering fields

  7. Field Parameters
    Breakdown of common field parameters

  8. Display Options
    Limit display of boxes

  9. Box Properties
    Breakdown of box properties

  10. Troubleshooting
    Common issues & how to deal with them

  11. Notable Changes in CMB2 (from original CMB)
    CMB2 — a complete re-write

  12. Tips & Tricks
    10x your CMB2 skills

  13. REST API
    CMB2 data through WordPress REST API

  14. Javascript API
    Work with CMB2 JS events & hooks

Advanced tutorials:

External Resources:

Clone this wiki locally