You can create a new taxonomy using the Awesome_Meta_Box
class, the
constructor for which accepts an array of properties. This array accepts a
number of various properties.
id
: required; the ID of the meta boxtitle
: required; the displayed title of the meta boxpost_types
: required; an array of post type IDs with which the meta box is associatedcontext
: optional; the part of the page where the meta box is placed (normal
,advanced
, orside
)priority
: optional; the priority within the context where the meta box should show (high
,core
,default
, orlow
)fields
: optional; an array of data fields that are to be displayed in the meta box
Assuming a new instance of Awesome_Post_Type
(with an id
of movie
) has
already been created:
$release_date = new Awesome_Meta_Box( array(
'id' => 'release_date',
'title' => 'Release Date',
'post_types' => array( 'my_movie' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array()
) );
As mentioned above, each Awesome_Meta_Box
instance must have an array of
fields, each of which represents a form field in the meta box. This array can
any number of field, each of which is itself an array.
Each field accepts the following properties:
id
: required; the value of the field'sid
attributename
: optional; the value for the field'sname
attribute. Defaults to theid
of the field.class
: optional; the value for the field'sclass
attributetype
: optional; the type of the field (text
,checkbox
,radio
,button
,submit
,image
,select
,textarea
, oreditor
). Defaults totext
description
: optional; the contents of thep
element which will precede the field. Thep
element is only prepended if this a value for this property is providedlabel
: optional; the contents of thelabel
element which will precede the field. Thelabel
element is only prepended if this a value for this property is providedplaceholder
: optional; the placeholder text to appear inside the field. Corresponds to the value of the HTML5placeholder
attribute.pattern
: optional; the pattern against which the field value will be matched upon submit. Corresponds to the value of the HTML5pattern
attribute.required
: optional; a boolean indicating whether or not the field is a required form field. Corresponds to the value of the HTML5required
attribute.populate
: optional; a callback function that outputs the custom HTML of the fieldbefore
: optional; a string of HTML to precede the field. Defaults to<p>
after
: optional; a string of HTML to follow the field. Defaults to</p>
Also note that you do not need to create or validate a nonce field; Awesome CPT will do so on your behalf.
Working off of the above example:
$release_date = new Awesome_Meta_Box( array(
'id' => 'release_date',
'title' => 'Release Date',
'post_types' => array( 'my_movie' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'id' => 'release_date',
'type' => 'text',
'label' => 'Release Date',
'placeholder' => 'Enter the movie release date here'
)
)
) );
As briefly mentioned above, the before
and after
properties determine what
HTML should precede and follow (respectively) any given field.
array(
'id' => 'release_date',
'type' => 'text',
'label' => 'Release Date',
'placeholder' => 'Enter the movie release date here',
'before' => '',
'after' => '<br />'
)
If you'd prefer to set the default before
and after
HTML for all fields,
modify the static field_defaults
array.
Awesome_Meta_Box::$field_defaults['before'] = ''
Awesome_Meta_Box::$field_defaults['after'] = '<br />'
Below are examples of the various input types you can create. For all of these types, Awesome CPT will handle the HTML escaping of values on your behalf.
array(
'id' => 'release_date',
'type' => 'text',
'label' => 'Release Date',
'placeholder' => 'Enter the movie release date here'
)
array(
'id' => 'review',
'type' => 'textarea',
'placeholder' => 'Enter a short review of the movie'
)
Note that Awesome CPT will automatically add a hidden
input to allow for the
proper saving of the checkbox value. This default unchecked value is off
.
array(
'id' => 'supports_3d',
'type' => 'checkbox',
'label' => 'Available in 3D?',
'value' => 'on',
'checked' => false
)
The following example will output a list of checkboxes for selecting the languages in which the movie is available.
Note that each field name
ends in a pair of brackets—this is to ensure the
checked values are properly saved. Also note that the example utilizes the
before
and after
properties to
array(
'id' => 'lang-en',
'name' => 'langs[]',
'type' => 'checkbox',
'label' => 'English',
'value' => 'en',
'checked' => true,
'before' => '<h4>Language</h4><p>',
'after' => '<br />'
),
array(
'id' => 'lang-es',
'name' => 'langs[]',
'type' => 'checkbox',
'label' => 'Spanish',
'value' => 'es',
'before' => '',
'after' => '<br />'
),
array(
'id' => 'lang-fr',
'name' => 'langs[]',
'type' => 'checkbox',
'label' => 'French',
'value' => 'fr',
'before' => '',
'after' => '</p>'
)
array(
'id' => 'my_radio_button',
'type' => 'radio',
'label' => 'My Radio Button',
'checked' => false
)
array(
'id' => 'my_radio_button',
'type' => 'radio',
'label' => 'My Radio Button',
'src' => get_template_directory_uri() . '/images/myimage.png',
'alt' => 'My Image'
)
array(
'id' => 'rating',
'type' => 'select',
'label' => 'Rating',
'placeholder' => 'Choose the designated rating for this movie',
'options' => array(
array(
'value' => 'g',
'content' => 'G'
),
array(
'value' => 'pg',
'content' => 'PG',
'selected' => true
),
array(
'value' => 'pg-13',
'content' => 'PG-13'
),
array(
'value' => 'r',
'content' => 'R'
)
)
)
Instead of passing an array of option arrays, you can also pass a function
reference to the options
property. The referenced function must return an
array in the same form as the one above (an array of option arrays).
See the example below, which modifies the previous example to dynamically generate a menu of ratings.
array(
'id' => 'rating',
'type' => 'select',
'label' => 'Rating',
'placeholder' => 'Choose the designated rating for this movie',
'multiple' => false,
'options' => function( $meta_value, $field, $post ) {
$ratings = array( 'G', 'PG', 'PG-13', 'R' );
$options = array();
foreach ( $ratings as $rating ) {
$options[] = array(
'value' => strtolower( $rating ),
'content' => $rating
);
}
return $options;
}
)
Again, note that the anonymous functions used above are only supported in PHP 5.3 and newer.
array(
'id' => 'my_date_picker',
'type' => 'date',
'label' => 'My Date Picker',
'value' => '2014-01-01'
)
array(
'id' => 'my_color_picker',
'type' => 'color',
'label' => 'My Color Picker',
'value' => '#000'
)
You can also specify custom HTML for your field by passing a function reference
to the populate
property. This allows for dynamic generation of your field to
fit your needs (such as querying posts or sending a request).
Note that you still need to specify the field's ID in the field array, though
you can access this in your populate
function for awesome reuse (via the
$field
parameter).
array(
'id' => 'release_date',
'label' => 'Release Date',
'populate' => function( $meta_value, $field, $post ) {
echo "<label for='{$field['id']}'>{$field['label']}</label>";
echo "<input type='text' value='$meta_value' id='{$field['id']}' />";
}
)
Also note that using the populate
callback will require you to escape the
variables you use.
You can also pass the value of a meta box field through a filter before being saved to the database or displayed in the field.
The meta value will be run through the save
filter before the meta data is
saved to the post object.
array(
'id' => 'release_date',
'type' => 'text',
'label' => 'Release Date',
'placeholder' => 'Enter the movie release date here',
'save' => function( $meta_value, $field, $post ) {
// sanitize value somehow
return $meta_value;
}
)
The meta value will be run through the display
filter before being displayed
array(
'id' => 'release_date',
'type' => 'text',
'label' => 'Release Date',
'placeholder' => 'Enter the movie release date here',
'display' => function( $meta_value, $field, $post ) {
// format value somehow
return $meta_value;
}
)