Working with Meta Boxes

Meta boxes are amazing for the WordPress world, whether you are creating them to integrate within a WordPress theme or a plugin. In this training, we will take an overlook of what you will need to do to create meta boxes for your site.

What is a Meta Box?

Meta Box Hooks

Meta boxes are creating using the add_meta_box() function, hooked in through the add_meta_boxes action like this.

add_action( 'add_meta_boxes', 'your_function_name' );

function your_function_name() {
      'metabox-item-id', // ID
      __('Title', 'my-metabox' ),  // Title
      '', // Callback
      'custom_post_type', // Screen
      'normal', // Context ('normal', 'side', 'advanced')
      'default' // Priority (default, 'high', 'low')

To ensure our meta boxes only show up where we would most likely wish them to show up, when adding or editing a post, we will amend that beginning add _action to fire only on those pages, like this:

/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'post_meta_boxes_setup' );

function post_meta_boxes_setup () {
    add_action( 'add_meta_boxes', 'your_function_name' );