Class Name:
MP_CORE_Metabox
Purpose:
To aid in the creation of metaboxes that save data for posts.
Overview:
This class adds a new metabox with fields of save-able data. The field can be singular or they can repeat in groups. It works by passing an associative array containing the information for the fields to the class
Usage:
MP_CORE_Metabox($add_meta_box, $items_array);
Parameters:
$add_meta_box – An associative array that has all the information necessary to create a new metabox.
$items_array – An associative array that has all of the information for each field within the metabox.
Example Metabox:
function my_meta_box(){ $add_meta_box = array( 'metabox_id' =--> 'my_metabox', 'metabox_title' => __( 'My Metabox Title', 'textdomain'), 'metabox_posttype' => 'post', 'metabox_context' => 'advanced', 'metabox_priority' => 'high' ); $items_array = array( array( 'field_id' => 'my_field_1', 'field_title' => __( 'My Text Area Field Title', 'textdomain'), 'field_description' => __('My Field Description:', 'textdomain'), 'field_type' => 'textarea', 'field_required' => true, ), array( 'field_id' => 'my_field_2', 'field_title' => __( 'My Color Picker Field', 'textdomain'), 'field_description' => __('My Field Description', 'textdomain'), 'field_type' => 'colorpicker', ), array( 'field_id' => 'my_field_3', 'field_title' => __( 'My Image Upload Field', 'textdomain'), 'field_description' => __('My Field Description', 'textdomain'), 'field_type' => 'mediaupload', ), array( 'field_id' => 'my_field_4', 'field_title' => __( 'My Select Field', 'textdomain'), 'field_description' => __('My Field Description', 'textdomain'), 'field_type' => 'select', 'field_select_values' => array( 'option1' => "Option 1", 'option2' => "Option 2" ), ), global $my_metabox; $my_metabox = new MP_CORE_Metabox($add_meta_box, $items_array); } add_action('plugins_loaded', 'my_meta_box');
Usage Breakdown:
Step 1. Create the metabox array
These arguments are very similar to the ones found the with add_meta_box function built into WordPress. Actually, we will be passing these values to that function – which is built into the class.
- metabox_id (required) – This is the id of the metabox. Make it something unique and relevant to your metabox but make it all lowercase with no spaces.
- metabox_title (required) – This is the title of the metabox that your users will actually see. Make this exactly as you want it to appear.
- metabox_posttype (default value is “post”) – This will determine whether this will display on a “post”, “page”, or a custom post type. Enter the value of the custom post type id if you want to use this there.
- metabox_context (optional) – This determines where the metabox will display on the edit screen. Potential options are ('normal', 'advanced', or 'side')
- metabox_priority (optional) – This is to determine how important this metabox is and thus, whether it is displayed before or after other metaboxes. Potential options are ('high', 'core', 'default' or 'low').
Step 2. Create the fields array
The properties that are included in this array are:
- ‘field_id’ – Required. This must be a unique name with no spaces
- ‘field_title’ – Required. This is the title of the field that will display to the user
- ‘field_description’ – Required. The user will see this description above this field
- ‘field_type’ – Required. See “Field Types” below.
- ‘field_required’ Boolean. True if the field is required to be filled out
- ‘field_repeater’ – Optional. This is the name of the repeater group. All fields that have this ‘repeater’ name will be grouped together and can be duplicated as one unit.
- ‘field_select_values’ – Optional. This is an array of the values you want to appear in a dropdown select menu.
Field Types
- ‘basictext‘ – This is a field that just displays text. Useful for giving instructions or displaying HTML previews.
- ‘textbox’ – Gives the user a simple field that displays and saves a Text Box
- ‘textarea’ – This is similar to the Text Box field but instead display a text area so the user can input paragraphs of information.
- ‘colorpicker’ – Gives the user the ability to choose colors using the new Iris color picker that comes with WordPress 3.5
- ‘mediaupload’ – This allows the user to upload or select an image using the new Image Management found in WordPress 3.5
- ‘select’ – This gives the user a dropdown menu to select a pre-set option
- ‘password‘ Creates a password field.
- ‘checkbox‘ – Creates a checkbox field.
- ‘url‘ – Creates a field that must contain a URL.
- ‘date‘ – Creates a field with a calendar dropdown so the user can choose a date.
- ‘time’ – Creates a field with a time input
- ‘number‘ – Creates a field that must contain a number.
- ‘input_range‘ – Creates a field with a range slider from 1 – 100
Step 3: Create the class.
global $my_metabox;
global $my_metabox = new MP_CORE_Metabox($add_meta_box, $items_array);
Notice that the 2 arrays we created above are passed into the class.
Using Saved Metabox Data:
To use a field, just use the get_post_meta function in WordPress with the $field_id.
get_post_meta($post_id, $field_id, true);
If you are trying to get a group of repeaters, the above won’t work as you need to use the $field_repeater instead:
$my_repeated_fields = get_post_meta($post_id, $field_repeater, true);
To loop through each repeater’s fields, do something like this:
foreach( $my_repeated_fields as $my_repeated_field ){ echo $my_repeated_field[$field_id]; }
To access a specific value in a repeated group, do something like this – where $number_of_repeats is the number of repeats deep you want to go. So if you want the value stored in the first repeater group, before the person has clicked “Add Another”, use 0 in place of $number_of_repeats. If you want a value from the second repeater, use 1. And so on for additional repeater’s levels and values within them.
echo $my_repeated_field[$number_of_repeats][$field_id];
Action/Filter Hooks:
mp_core_metabox_custom_scripts
mp_title
mp_description
mp_core_customfieldtype