Skip to content

Adding meta fields to a meta box

nadavrt edited this page May 28, 2015 · 18 revisions

Each meta field is represented by an array, and should be included inside the fields key of the desired metabox. Each field you add to your meta box can accept several keys (arguments).

Available Field Keys

id - String. Required. The field id. Should be in slug form and must be unique to this field AND to this box. You cannot use the same field id in different boxes. Example: my_field

title - String. Optional. The title for the field's label. Appears to the left of each field. This is what the end user will see.

description - String. Optional A description for the field. Will appear below the actual field.

class - Array. Optional. An array of classes that the field will inherit. Can be used for all field types except wysiwyg.

required - String. Optional. Add this key to make this a required field. The value of this key should be the string to display if the required field was not filled (or was filled incorrectly). The following field types does not support this feature due to it being redundant: radio,select,colorpicker.

repeater - Boolean. Optional. Set to true to make this field a repeater.

type - String. Optional. The current field type. The available field types are:

  • text
  • email
  • textarea
  • checkbox
  • radio
  • select
  • colorpicker
  • file
  • wysiwyg

The default field (in case one is not specified) is text. Any illegal field type will be interpreted as a text field. For more information on field types see field types.

Field Types

'text'

Creates a text input. By default it is not sanitized and can contain HTML and code. If you wish to limit a text field to plain text only pass it through the plain_text sanitation filter. The id and class sanitation filters may also come in handy depending on the situation. Example:

array(
    'title'    => 'Production Studio',
    'id'      => 'production_studio',
    'type'    => 'text',
),

email

Creates an email addresses. The field will be validated using PHP and will only accept a valid email address. If you set this field to be a required field it will also be validated as a valid email by the javascript. Example:

array(
    'title'    => 'Studio Email',
    'id'      => 'studio_email',
    'type'    => 'email',
),

textarea

Creates a text area. Example:

array(
    'title'    => 'Short Description',
    'id'      => 'short_description',
    'type'    => 'textarea',
),

checkbox

Creates a checkbox selector. The checkbox field type requires an additional key called choices. choices is an array that contains the key and value pairs of the checkboxes that will be rendered by this field. Example:

array(
	'title'    => 'Genres',
	'id'      => 'movie_genres',
	'description' => 'Select the movie genres.',
	'type'    => 'checkbox',
	'choices' => array(
		'action' => 'Action',
		'comedy' => 'Comedy',
		'drama' => 'Drama',
		'documentary' => 'Documentary'
	),
),

radio

Creates a radio selector. The checkbox field type requires an additional key called choices. choices is an array that contains the key and value pairs of the radio buttons that will be rendered by this field. Example:

array(
	'title'    => 'Released on Blu-ray',
	'id'      => 'released_on_bd',
	'type'    => 'radio',
	'choices' => array(
		'yes' => 'Yes',
		'no' => 'No',
	),
),

select

Creates a select selector. The select field types requires an additional key called choices. choices is an array that contains the key and value pairs of the select selector that will be rendered by this field. Example:

array(
	'title'    => 'Good Movie Snack',
	'id'      => 'movie_snack',
	'type'    => 'select',
	'choices' => array(
		'popcorn' => 'Popcorn',
		'chocolate' => 'Chocolate',
		'none' => 'None',
	),
),

colorpicker

Creates a colorpicker box. Example:

array(
    'title'    => 'Movie Page Color',
    'id'      => 'movie_color',
    'description' => 'Select a color that represents this movie.',
    'type'    => 'colorpicker',
),

file

Creates a file-uploading button. By default the file field type accepts the following formats: jpg,jpeg,png,gif,pdf,txt,docx,xlsx and pptx. To add new formats add the desired format value to the $permittedUploadFormats array property of the Simple_Meta_Boxes class. The file field type can also show a preview image for image type files. It will do so by default for the following formats: jpg,jpeg,png and gif. You can add a new preview-enabled file format by editing the $imageFormats array inside the add_file_image method. Do take note that only image formats that can be rendered natively by the browser can be used. Example:

array(
	'title'    => 'Movie Poster',
	'id'   => 'movie_poster',
	'type' => 'file',
),

wysiwyg

Creates a wysiwyg textarea. wysiwyg fields require the corresponding metabox to be seamless and cannot be used as a repeater field or as part of a repeater group. If a wysiwyg field is used incorrectly it will default to an input (in repeater fields) or textarea (all other occasions). The wysiwyg field supports an additional key called args. Args is an array of optional arguments that the field can accept. These arguments are synonymous with those $settings array used by the Wordpress core function wp_editor. Example: array( 'title' => 'Movie Review', 'id' => 'movie_review', 'type' => 'wysiwyg', 'args' => array( 'media_buttons' => FALSE, //hide the media buttons ), ),

Clone this wiki locally