Register Meta Boxes

To register meta boxes, copy and paste the code below into functions.php file of your theme or your plugin:

Note: Since 4.8.0 the code to register meta boxes and custom fields must run in both admin area and frontend. Do not add conditional checks before or inside the callback function. For example, do not write as follows:

if ( is_admin() ) {
    add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    function prefix_register_meta_boxes() {
        // Code
    }
}

instead write:

add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
function prefix_register_meta_boxes() {
    // Code
}

Filter

The Meta Box plugin uses rwmb_meta_boxes filter to register meta boxes. This is a custom filter which takes an array of defined meta boxes as the argument. The callback function must return an array of meta boxes.

Using filter prevents PHP errors if you accidentally forget to install or activate the Meta Box plugin. In that case, the code simply doesn’t run and doesn’t affect the rest of your website.

Prefix

In the beginning of the code, we defined a prefix for fields: $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.

Tips:

  • The prefix is optional. If you don’t want to use it, set it to an empty string or remove it (if you remove it, don’t forget to update id of fields).
  • Use underscore (_) at the beginning to make the custom fields hidden, e.g. they won’t show in the default WordPress Custom Fields meta box.
  • It’s a good practice to have an underscore (_) as last sign.

Meta Box Attributes

Each meta box has the following attributes:

Name Description
id Meta box ID. Optional. If it’s absent, it will be generated from title using sanitize_title function.
title Meta box title. Required.
post_types Custom post types which the meta box is for. There can be an array of multiple custom post types or a string for the single post type. Must be in lowercase (like the slug). Optional. Default: post. This parameter is used instead of pages since version 4.4.1 (and fallback to pages for previous versions). See change log.
context Part of the page where the meta box is displayed (normal, advanced or side). Optional. Default: normal.
priority Priority within the context where the box is displayed (high or low). Optional. Default: high.
default_hidden Hide the meta box by default (true or false)? The meta box can be toggled using the checkbox option in screen Help (on the top right). Optional. Default false.
autosave Auto save the custom fields’ values (like post content and title)? Optional. Default: false.
fields Array of fields. Each field is declared as an array with its parameters. To understand fields’ parameters, please read this documentation.

Historical note

Previously, you could register meta boxes using admin_init hook and a global variable as below:

However, this method has some disadvantages:

  • You have to remember the plugin class name (RW_Meta_Box) to the check if it’s loaded or not which might be changed in the future.
  • If there is another code that uses the same global variable to register meta boxes, be careful that the global variable can be overwritten.
  • It works in the admin area only
  • It’s not compatible with some premium extensions such as Include Exclude, Settings Page, Term Meta.

Using rwmb_meta_boxes filter as described above, you will have more benefits:

  • The code is more elegant and easier to read.
  • There is no more global variable, so you never overwrite registered meta boxes by accident. If you want to edit or change existing meta boxes, there’s an instruction for you.
  • It’s compatible with all premium extensions.
  • Works in both frontend and backend

IMPORTANT: Since version 4.8.0, the rwmb_meta function won’t work in the frontend if you use this method. In order to get the meta value, you have to manually use get_post_meta() function. For more details, please look at this documentation.

Get all extensions now and save > 50%!