Functionality
You can install Meta Box from the wordpress.org plugins repository or you can load it via composer. After installation you’ll be shown an introduction page. Other than this page, there’s no admin/settings/options page for Meta Box at all. You’ll need to setup the fields manually inside your functions.php file. Here’s how the API for that looks like:
add_filter( 'rwmb_meta_boxes', 'prefix_meta_boxes' );
function prefix_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'title' => 'Test Meta Box',
'post_types' => array( 'page' ),
'fields' => array(
array(
'id' => 'name',
'name' => 'Name',
'type' => 'text',
)
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'your_prefix_get_meta_box' );
The API is straightforward. You’ll need a function where you can define the fields as an array. For those who’ve used Carbon Fields, this is pretty similar. It may look a lot of work in the beginning but its pretty simple once you get the hang of it.
There’s also an online form generator which can help you greatly speed up field group creations. This is super useful as coding large field groups can be tedious.
Once you’ve added the code above in your functions.php, create a new page and you should be able to see a textbox metabox underneath the wysiwyg field.
To grab the data entered here, add this in your template:
echo rwmb_meta(<field_id>);
That’s how you can get and set data via MetaBox. You can also show data via shortcodes which can be pretty useful.
The core MetaBox plugin comes with over 40 plugins. That’s quite a lot and even more than most Custom Field plugins.
Some of the fields are:
- Text
- WYSIWYG
- Image
- Image select
- Button
- Custom HTML
- Select Advanced
- File input
- Map
- OEmbed
- Date Time
- Post
- Taxonomy
- User
Out of these, we really like the button and the HTML field. The button allows you to bind it to custom actions via their API. There are many thing you can do with this, eg; you can have a button field on the post page which submits the post to twitter/facebook/ifttt, etc when clicked. Or you can trigger a spellchecker/wordcounter function on the edit page which parses all the metaboxes in the post page.
The HTML field allows you to write markup. So you can possibly add instructions for content writers inside the markup and style it a bit. Or when you’re including MetaBox inside a plugin, you can show notifications via the HTML field.
One glaring omission is the repeater field which can create repeatable field groups. There’s options to set a clone option to fields where you can retrieve values as an array but if you need a full blown repeater field, its available as a paid extension.
By default you can only attach field groups to posts and pages. There’s no option to attach them to taxonomies, users or even custom post types by default. The only way to do this is to install extensions.
The philosophy of MetaBox is to keep the core plugin lightweight. Any extra functionality you need, you can install it as an extension.
Styling metaboxes
There are options to show fields in a column but for more advanced styling you’ll need to enqueue an admin css file to apply your changes.