In a previous post I talked about ACF and CMB, going over the pros and cons of each. In that post I mentioned that while both are easy to install and operate, CMB takes a little more time and could become confusing for newer devs. Well, today we’re going to clear all that up. We’ll be taking some time to go over the simple process of setting up and getting running with the great library created by Jared Atchison.
Where Most Go Wrong
So in most articles geared toward newer devs the library is added to your WP build is by putting it in a plugin. There is nothing wrong with doing this, and has its own benefits and scenarios for it.
- CMB is a cog in the code for an actual plugin
- Plugin is a general compilation of reused code in all your themes
Outside of that, its not very smart to include it that way. Now that we’ve done a brief pit stop into how this way is wrong, lets go over our steps to doing it the better way.
Step 1: Download/Clone Library from GitHub
Starting with the very first necessary step, you have to get the code down from GitHub. Here’s the link.
Step 2: Placing it in Your Theme Folder
Okay, we now have gotten the code library down from the Github respiratory and its unzipped. Now what we have to do is get it into our theme directory. To do this we first go into the root of our theme and create a folder called lib. Lib is short for library and this will be the parent folder for all code libraries that will used for your theme. Then you create the directory metabox inside of lib, this new folder will be where we place all the code for CMB. All easy stuff!
Step 3: Creating Your Post Types
Okay so I’m assuming that if you’re reading this that you don’t need a breakdown in how to create custom post types. However, when you add a library like CMB the traditional architecture used for creating them is a little stupid. The reference being made here is putting your custom post type function in your functions.php file.
A better practice for including custom meta boxes is to group them with their respective post type declaration in one file. Of course this wouldn’t be in your metabox directory, instead create a new directory called something like posts to store all those php files. A good template for this is the example php file the CMB library provides.
Step 4: Deciding Filter Options
Now that we got our post types set up, let’s now figure how we want to filter our metaboxes’ display. There are three defined options for doing this: post type, template, and custom filter. The first two are relatively easy and require no extra code work, so we’ll touch on those first. Post type filtering is easy as can be. As stated in the documentation on GitHub, all you have to know is the name of your post type(s).
<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;">This couldn't be any easier. As a quick note, if you wanted to show your custom fields on more than one post type than just add a new entry inside the array holding values for 'pages'. To filter for templates and post id's, you'll need to make us of the </span><em style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;">show_on </em><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;">filter.
Step 5: Including
The last step is a simple include statement. To better facilitate multiple custom post types, you create one include file where you include all your custom post files in your metabox directory and then include that one file in your themes functions.php file.