Intro to WordPress Meta Boxes

By Rob Gravelle

Intro to WordPress Meta Boxes

In WordPress, post information is edited within special components called meta boxes. Their purpose is to allow the user to edit or enter additional meta information that is in addition to the main post content. Existing meta boxes may be modified in terms of content and/or positioning to customize your Editor screens. In today's article, we'll learn how to work with WordPress's native meta boxes to customize them in a variety of ways.

Anatomy of a Meta Box

You can recognize a meta box by its distinct title and content sections:

The content may be comprised of just about any HTML content, from displayed text, links, to input fields. Some of the standard meta boxes are shown in the screenshot below. They include Format, Categories, Tags, Featured Image, and Publish:

Meet the Players

In order to identify WordPress's meta boxes, you'll have to familiarize yourself with their unique IDs. Some of the most prevalent ones include:

  • 'authordiv' - Author metabox
  • 'categorydiv' - Categories metabox.
  • 'commentstatusdiv' - Comments status metabox (discussion)
  • 'commentsdiv' - Comments metabox
  • 'formatdiv' - Formats metabox
  • 'pageparentdiv' - Attributes metabox
  • 'postcustom' - Custom fields metabox
  • 'postexcerpt' - Excerpt metabox
  • 'postimagediv' - Featured image metabox
  • 'revisionsdiv' - Revisions metabox
  • 'slugdiv' - Slug metabox
  • 'submitdiv' - Date, status, and update/save metabox
  • 'tagsdiv-post_tag' - Tags metabox
  • 'trackbacksdiv' - Trackbacks metabox

Removing Meta Boxes

Of course if you are an Admin user, you can select exactly which meta boxes you'd like to see by pulling down the Screen Options panel at the top of the page and checking the boxes beside the ones you want.

While that certainly works, you may want to remove meta boxes automatically for non-admin users in order to exert better control over what they can and cannot do. While there are a number of plugins for managing meta boxes, you can do it yourself through code. As an example, say that you didn't want users to be able to assign an image to their posts. A good way to prevent that from happening would be to simply remove the Featured Image meta box from the editor. WordPress provides a function specifically for that purpose called remove_meta_box(). It has the following signature:

remove_meta_box( $id, $page, $context );

The $id is one of those listed above. The one for the Featured Image meta box is "postimagediv".

The $page argument is required and is a string representing the type of screen from which to remove the meta box, including:

  • 'post'
  • 'page'
  • 'attachment'
  • 'link'
  • 'dashboard'

The $context refers to a meta box's position within the document. It may be one of three constants:

  • 'normal'
  • 'advanced'
  • 'side'

We'll be delving more into meta box positioning in the next section, but for now, suffice to say that normally positioned meta boxes go just below the main editor, while advanced ones go below those. Side meta boxes are all those which are found in the second column on the right. It just so happens that the Featured Image Meta Box is on the side.

Hence, here's the call to remover_meta_box() that would remove the Featured Image Meta Box from the Post Editor:

remove_meta_box( 'postimagediv' , 'post' , 'side' );

For best results, the above call should be made from within the functions.php file or plugin. There, you can bind your code to the admin_menu action hook:

function remove_publish_box()
{
        remove_meta_box( 'postimagediv' , 'post' , 'side' );
}
add_action( 'admin_menu', 'remove_publish_box' );

Adding Meta Boxes

Just as you can remove meta boxes from an edit screen, it is also possible to include meta boxes that aren't normally displayed in that particular editor. The function that does that is called add_meta_box. It's similar to remove_meta_box() but includes a few additional parameters:

add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );

The $id works exactly the same way as for remove_meta_box().

The $title parameter sets the text in the Title section of the meta box. As with any displayed text, the value should be wrapped within the __( ) internationalization construct.

The $callback function is the one responsible for constructing the HTML meta box content. Your callback function can accept up to two arguments: the $post object and an array of optional additional arguments for your function.

The $post_type is exactly the same as the $page argument of the remove_meta_box() function.

The $context is also the same as the $context argument of the remove_meta_box() function.

The $priority works in conjunction with the $context to control the positioning of the meta box. While the $context argument will place the meta box in a general area, the $priority argument will allow for greater control of where the meta box displays within the $context area. It can be one of 'high', 'core', 'default' or 'low', with 'default' being ascribed if omitted. Note that, if two meta boxes have identical $context and $priority, the meta boxes will be arranged alphanumerically according to the $id argument, with "a" and "0" being highest and "z" and "8" being the lowest.

The $callback_args is an optional array of arguments to pass into your callback function. Hence, the callback will receive the $post object and whatever parameters are passed through this variable.

With that in mind, here is the call to add a comments meta box to the post editor:

add_meta_box('commentsdiv', __('Comments'), 'post_comment_meta_box', 'post', 'normal', 'core');

Conclusion

In today's article we learned how to add and remove meta boxes from WordPress editor screens. Next time, we'll explore how to arrange meta boxes in a one column layout rather than two.


Rob Gravelle resides in Ottawa, Canada, and is the founder of GravelleWebDesign.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses.

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His band, Ivory Knight, was rated as one Canada's top hard rock and metal groups by Brave Words magazine (issue #92).



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date