Reusable Custom Meta Boxes Part 1: Intro and Basic Fields

There are a lot of tutorials on creating custom meta boxes, even just here on Wptuts, and it goes to show that having a good knowledge of these and a system for creating them is crucial to a successful WordPress theme or plugin project. In this series we won’t rehash what meta boxes are but instead we’ll create an easy system for plugging them into whatever your latest project is to keep your work moving consistently.


Create a Meta Box

As usual, the first step is to set up our code for creating a meta box. We’re just creating a library that we can come back to, so we’ll use a really generic name for everything. Some settings can also change whether you’re coming from a theme or a plugin, but the simplest way is to build it into a theme, so that’s what we’ll cover here. All of our code will either be placed into functions.php, or in a file that you include from functions.php in your theme.

This will add a meta box to the Write/Edit Post screen.

  • The $id will be added to the box as an id to make it easy to reference in style sheets and other functions
  • The $title will be displayed in the handle of the box
  • The $callback is the function we’ll use to define the output inside the box
  • $page is used to select which post type the box will be used on
  • We use $context to determine where the box will show up on the page
  • Having $priority set at “high” will put the box as close to the editor as we can and factors in other boxes added by core and plugins.

Create the Field Array

Since we’re building something that we can use over and over again, we won’t be defining the html for every field. Instead, we’ll create an array that holds all of the unique information for each field including a “type”. Then we’ll loop through each field and change the html output according to the type.

It’s important that your meta fields have a unique key so a $prefix is defined to make it simple to attach the same one to each field id. Then we begin an array of arrays where each item defines a new field with a label, description, unique id, and the type of field it is. The select box has additional data to add all of the options for the select box. Using this template, you can add as many of each type of field you want. The order in which you add them to the array is the order in which they will output in the meta box.


Outputting the Fields

Now we are ready to set up our callback function and being outputting the html for each field.

This code sets up the callback with a table for the fields and loops through each field in a table row.

  • Echo a hidden nonce field to verify the fields when we save them later
  • Start a table and begin a loop through each field from the $custom_meta_fields array.
  • Get the value of the field if it has been saved for the current post already so that we can output it in the field
  • Begin a table row with two cells: a <th> for the label of the field, and a <td> for the field itself.
  • Then we’ll insert our switch case items.
  • Finally, end the table row, the loop, and the table before closing the function

Case: Text Input

The basic idea here is to test what the field type is and change the output accordingly.

The idea here is pretty simple.

  • If the field type is “text”, echo the html code using that field’s array settings
  • The field id is used for the name which will create the meta field’s unique key, and it’s used for the field id which we want so that we can link our label to the field and also call it in our style sheet later if we want
  • The value will output the $meta variable which is empty if this field hasn’t been saved for this post yet.
  • After the field, we’ll output the description for further explanation of what is expected

Case: Textarea

This follows the same principles as with the text field, except follows the standard of textarea html. The $meta is put between the opening and closing tags so that it outputs any saved text.

Case: Checkbox

A check box can be a little tricky because the value of $meta is used to determine whether or not the box should be checked. In our code we use an inline conditional that outputs the “checked” attribute if the $meta value exists, and nothing if it doesn’t. The other difference here is that we use the field’s description in another label so that there’s a large clickable area for the user.

Case: Select Box

The select box is tackled in a completely new way.

  • Open the select field
  • Loop through each of the options that we defined in our array
  • Use an inline conditional to determine if the current option is the one saved for the post and output the “selected” attribute if it is
  • Close the select field and add the description

Save the Data

We’ve set up our basic box with a nice array template for using and reusing multiple types of fields. Now we need to loop through them again, verify them, and save them to the post.

First we’ll go through a few security checks to see if the fields should be saved; nonce, autosave, and user capabilities are all checked. Then we loop through each field again.

  • Get the field’s value if it has been saved before and store it as $old
  • Get the current value that has been entered and store it as $new
  • If there is a $new value and it isn’t the same as old, update the post meta field with the $new value
  • If the $new value is empty and there is an $old value, delete the post meta field $old value
  • If there are no changes, nothing happens

Conclusion

Your final box should look like the one pictured here:

Custom Meta Box

We have started ourselves a nice little library and template for creating a custom meta box with multiple arrays. Now you can easily drop this into any theme or plugin and use the array to add as many text fields, textareas, single checkboxes, and select boxes as you need. In the continuing parts of this series, we’ll begin working with more advanced fields and even a couple fun ones that involve jQuery UI. You won’t want to miss them!

Tags:

Comments

Related Articles