If you’re reading this post, chances are you have used CMB2 to make awesome meta boxes. Or maybe you are considering using CMB2 to create meta boxes. The gist of CMB2 is simple. This library is awesome! It is a developers dream for making meta boxes through the different WordPress types. All of the special field types that you used to have to build from scratch using html and js are now as easy as setting a few keys. (Disclaimer: I am not the author of CMB2. While I have contributed to the project, I cannot take most of the credit)
The Rub: If you have used this library before or have just read through the docs you will see the options are enormous. The boxes have options, the fields have options, the types of fields have different options. Trying to keep track of all the possible field types and options for specific fields is impossible. You can memorized a few over time, but you’ll find yourself running to the docs often to be able to use it.
This was not good enough for me. I decided to do something about it. Meet wordpress-lipe-libs. There are a ton of utilities available here which are not related to this post so I won’t get into all that. This library is a compilation of common utilities and wrappers for doing WP boilerplate tasks in an OOP way. For the context of this post you will be interested in the /src/CMB2 directory.
Within this directory you are going to find classes which map out every possible option for every box, field, and content type. You set options by setting properties and calling methods. Using an IDE with autocomplete like PhpStorm means you don’t have to memorize a single option. You simply start typing what you think it might be and the IDE will find it for you.
Lets see this in action: Say you want to add new meta box to pages with a few custom check-boxes and a select all check-boxes option. (For the sake of brevity in the example I’m leaving out translation calls and name-spacing. I’m also using a functional pattern to make the example more readable.)