Skip to content

Adding meta boxes

nadavrt edited this page May 29, 2015 · 10 revisions

Each metabox is an array with several keys. These keys give you the ability to control the metabox's various properties, such as where and how it should appear, what title it will have and so on.

Available metabox keys

id - String. Required. The metabox id. Should be in slug form and must be unique to this box. Example: my_new_box

title - String. Required. The title of the metabox. This is what the end user will see.

post_type - Array. Optional. An array of the post types that this metabox should appear on. Example: array( 'customer_forms', 'tv_reviews')

page_templates - Array. Optional. an array of page templates that this metabox should appear on. Each template must be written according to its full file name. Example: 'page_templates' => array('page-special.php', 'page-faq.php')

default - String/Array. Optional. A default for this field. Notes:

  • All field defaults should be strings, except for the checkbox field defaults which should be provided as an array.
  • Defaults cannot be specified for the select field type.
  • colopickers should have their default color entered with the hash tag sign. Example: #359337
  • The Simple Meta Boxes class will revert to the default option if a field is empty and if that field is saved with an empty string ('') as its value. If you wish to save an empty string instead of the default value either remove the default property, change the default property to an empty string or input a string with a backspace (' ') in the field before saving it.

seamless - Boolean. Optional. Add this key to make the metabox seamless. Seamless metaboxes don't have a background or handle and as a result integrate seamlessly into the CMS content flow. However, a seamless metabox will not have a title (since it needs to be indiscernible from the rest of the Wordpress content) and cannot be closed/hidden from view or moved (since it doesn't have a handle).

repeater_group - Array. Optional. Defines if the metabox should be a repeatable group (see repeatable groups). Defaults to FALSE.

fields - Array. Optional. An array containing all the custom fields arguments for this metabox. See Adding meta fields to a metabox for more information on custom fields.

Examples

Creating a metabox with one custom text field that will be displayed on movie post types:

$metaboxes['movie_information'] = array(
	'id'          => 'movie_information',
	'title'       => 'Movie Information',
	'post_type'   => array( 'movies' ),
	'description' => 'You can add the movie\'s information here.',
	'fields'      => array(
        array(
		    'title'    => 'Production Studio',
		    'id'      => 'production_studio',
		    'type'    => 'text',
		),
	)
);

Creating a metabox with one custom colorpicker field that will be displayed on pages associated with the page-special.php page template.

$metaboxes['special_settings'] = array(
	'id'          => 'special_settings',
	'title'       => 'Special Settings',
	'page_templates' => array( 'page-special.php' ),
	'fields'      => array(
        array(
		    'title'    => 'Page Color',
		    'id'      => 'page_color',
		    'description' => 'Select a color that represents this page.',
		    'type'    => 'colorpicker',
		),
	)
);

Clone this wiki locally