In the past I used to use ACF as the field system for many of my plugins. I’ve been a programmer since before there were field systems, and before there were CMS systems. I’m just really, really old now lol. Point is, I don’t see the point of spending the time creating forms and fields. That stuff has been done, and done again, and odds are when you reinvent the wheel that way you’ll make mistakes. Either ACF or Meta Box take care of infinite numbers of things you would never get around to when building your own field handling system.
I’ve recently switched from ACF to Meta Box. And I’m hugely excited about it, because as I pointed out in a recent post Meta Box is awesome! In this post I’m going to share with you the overall idea of how to use Meta Box in your plugin development. It’s an overview, so I’m not going to give you a lot of details, I’ll be posting code samples and more details on this approach later. If you’re a seasoned WP Plugin developer odds are you can read the Meta Box docs and piece things together yourself… what I want to share is the strategy, the methodology behind using Meta Box within a plugin.
Here is a common scenario in many plugins, written as requirements:
- Custom Post Type (CPT) such as “animals”
- Animals have distinct data fields such as “Number of Legs”. Your goal is partly to make managing the animals easier, and partly to better support displaying the animals.
- Provide a custom single template for each animal post
- Provide an archive page that is custom and in some form lists all the animals, possibly just a directory style list, but possibly a more advanced approach with search and filters.
- Other possible requirements:
- Support searching by meta such as finding all animals with 7 legs. By the way there are many animals with 7 legs, they’re called the spiders that escaped my foot.
- Provide front-end submission of animal posts
- Display animal posts in a widget or shortcode
- Organize animal posts with a custom taxonomy
- Connect animals to another CPT as either parent or child, for example an animal rescue agency might want to have “orphans” (pets available for adoption) which is referenced from the animals post
First off let’s look at where Meta Box would come into play in the above. It won’t do every one of these things, but it will support you in building out all the requirements listed above.
- Registering the CPT. You can register your CPT with the Meta Box Post Types extension. During development can register it using the interface, then export it to PHP and bundle that export into your plugin. If the plugin is for a single website though, you might continue using the interface driven post type instead. Personally I would always export simply because I want any plugin I build to be portable. The Meta Box Post Types extension to me is a utility tool, a valuable one that saves me time. But I won’t rely on it for the finished product.
- Meta Box Post Types extension can provide any taxonomies used.
- Meta Box provides the fields for the CPT, organized into metaboxes. Meta Box extensions can be used to improve the interface such as adding tabs, or repeating groups of fields. For me those two features are must-haves in most plugins I build.
- Meta Box can provide the front-end form for submission with an extension.
- Meta Box can create the references between the CPT and another CPT using post type fields, and with extensions there are different field and selection options.
- Meta Box provides functions to get field values which you’ll use in your custom templates.
Meta Box basically does most of the heavy lifting here to create the features I listed in the requirements above. Where you’ll need to fill in the gaps are mainly the template handling. I like to use a template override system which I first created for the WordPress quiz plugin QuizMaster. It’s been refined across many projects, and in it’s current version is integrated into Clay Framework. What it does is search the site active theme and find a template override, and if none are found it loads the default template within your plugin. You’ll need to build these default templates, using Meta Box get value functions to retrieve values and determine logic such as switching display based on values.
Imagine being able to cut as much as 80% of the work down on your next plugin build. That’s the power of Meta Box integration for WordPress plugin development. Have fun building, and stay tuned for follow-up posts where I’ll go more in-depth into implementing Meta Box features in a real plugin build.