Skip to content

Adding meta boxes

Nadav Rotchild edited this page Mar 31, 2017 · 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.

show_title - Boolean. Optional. Defaults to TRUE. Change to FALSE to hide the metabox title. Bear in mind that if you set show_title to FALSE your metabox will not have a handle. Metaboxes without a handle cannot be moved or minimized. The effect of hiding the title is best when combined with the seamless key.

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 cannot be minimized or moved (since it doesn't have a handle).

post_type - String/Array. Optional. The post type or an array of the post types that this metabox should appear on. Example: array( 'customer_forms', 'tv_reviews').
To include the metabox in all post types give the post_type key the value of post.

page_templates - String/Array. Optional. The page template or 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').
To include the metabox in all page templates give the page_templates key the value of page.

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.

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',
        ),
    )
);

Creating a metabox that will be displayed on all posts and pages.

$metaboxes['seo_fields'] = array(
    'id' => 'seo_fields',
    'title' => 'SEO',
    'post_type' => 'post',
    'page_templates' => 'page',
    'fields' => array(
        array(
            'title' => 'Meta Title',
            'id' => 'seo_meta_title',
            'type' => 'text',
        ),
        array(
            'title' => 'Meta Description',
            'id' => 'seo_meta_description',
            'type' => 'text',
        ),
        array(
            'title' => 'Meta Keywords',
            'id' => 'seo_meta_keywords',
            'type' => 'text',
        ),
    )
);