Introduction

The present article will discuss the finer details of adding meta boxes to WordPress write screens. It will cover the WordPress functions involved in the process, as well as give in depth information about the necessary hooks involved in the process. You will learn how to display and save meta boxes. After working through this article, you will have the capability of adding your own meta boxes to your projects!

Displaying Meta Boxes with the add_meta_box Function

The add_meta_box function is the primary function that allows for information to be added to the add/edit screens for WordPress post types. This powerful function is easy to leverage and can lead to customized content being added to WordPress write screens.

The add_meta_box function takes seven arguments. The arguments with each argument’s data type, required/optional status, and default value are listed below.

Argument Data Type Required Optional Default
$id string X No default
$title string X No default
$callback string X No default
$page string X No default
$context string X advanced
$priority string X required
$callback_args array X null

To understand the purpose and influence of each argument, each will be discussed separately.

$id

The $id argument defines the internal name for the meta box. WordPress core determines order to display meta boxes and screens to display meta boxes on based on this argument. Internally, this reference to the meta box is utilized in numerous arrays to reference the meta box. Obviously, this is a crucial piece of information to send to the add_meta_box function. Externally, the value of the argument is used for the id attribute in the div that encompasses the meta box. Changing this value has no effect on the display of the meta box unless CSS rules have been applied to the meta box using the id attribute. While the add_meta_box will execute with any string, it is advisable that you only use characters that you would normally use for HTML id and class attributes: 0-9, a-z, ‘-’, and ‘_’. Spaces should also be avoided. It is also recommended that the $id argument is prefaced with a unique identifier. For example, in building a plugin called “Weather Report” that adds a meta box for attaching the current temperature to a post, it would be sensible to add “wr” to the beginning of the $id value. As such, an appropriate $id value would be “wr_weather_box”. By prefixing the $id value, clashes with other plugin utilizing similar values will be reduced and many headaches will be avoided.

Usage Tips:

  • Use only the following characters: 0-9, a-z, ‘-’, and ‘_’
  • Prefix with a unique string that represents your plugin or theme (e.g., “wb” for “Weather Box”

$title

The $title argument is only slightly more interesting than the $id argument. The $title argument controls the value that will be printed for the meta box label. This argument will find itself nestled nicely inside a span and h3 tag within the main meta box div. For instance, if the $title argument is set as “Current Weather”, the following HTML would be generated:

 

</code>
<div id="highlighter_512919" class="syntaxhighlighter  ">
<div class="lines">
<div class="line alt1">
<table>
<tbody>
<tr>
<td class="number"><code>001</code></td>
<td class="content"><code class="plain"><</code><code class="keyword">h3</code> <code class="color1">class</code><code class="plain">=</code><code class="string">'hndle'</code><code class="plain">><</code><code class="keyword">span</code>&lt;code class="plain">>Current Weather</</code><code class="keyword">span</code><code class="plain">></</code><code class="keyword">h3</code><code class="plain">></code></td>
</tr>
</tbody>
</table>
</d