The code below registers multiple meta boxes at once, put this into
functions.php file of your theme or your plugin:
In the code above, we hook into
rwmb_meta_boxes, this is a custom filter of the plugin, which takes an array of defined meta boxes as the argument. The callback function must return an array of meta boxes.
Historical note: Using this filter instead of old way using
admin_init hook has some benefits:
- You still have all benefits from
admin_init, like other useful information when the admin area is ready. For example, post ID, information about current screen etc. which can be used later in some conditions.
- It’s loaded only in the admin, so you will have good performance in the frontend.
- You don’t have to remember the plugin class name or the check if it’s loaded or not.
- The code won’t brake if you forget to install the plugin (it just doesn’t run).
In the beginning of the code, we defined a prefix:
$prefix = 'rw_';
It will be added before all of our custom field IDs. Using a prefix can prevent us from conflict with other scripts that also use custom fields.
Note that this prefix is optional. You can delete it if you want, and remember to remove it from the field IDs in that case.
Now let’s talk about meta box attributes. Each meta box has the following attributes:
||Meta box ID. Optional. If it’s absent, it will be generated from
||Meta box title. Required.|
||Custom post types which the meta box is for. There can be array of multiple custom post types or a string for single post type. Optional. Default:
||Part of the page where the meta box is displayed (
||Priority within the context where the box is displayed (
Each meta box has a list of fields, which represent itself an array. Each array contains information about one field. To understand how to define fields, please read this documentation.