Shelley Bassett



Creating and Saving Custom Metadata for a post

June 18, 2018

Custom metadata is a really nifty way to add extra information across all posts or custom post types. It lets you limit or free the formatting, and call it using hooks in other pages. But how do you add this functionality to WordPress?

By utilising custom metadata, posts or custom post types can store additional information in the database to be called during a loop. This might be an image, a product description, or the url to a an external page. Whatever type of metadata you need to include, there’s probably a way to do it.Creating a custom area in a post page requires a bit of code, and if you need to preserve this metadata when the site theme changes, be sure to use a custom plugin. Otherwise, all of the following code and adjustments can be made directly in the functions.php  file of your theme. This tutorial will walk through the steps to adding a rich text box, just like the one used to write a blog post.

Creating custom metadata

There are three functions to add metadata to posts. One to create the data entry site, one to hold the content, and one to save it to the database. Together, they look like this:

Breaking down the function

Data Entry

Each piece of custom metadata needs a place to be added. This is normally on the add or edit posts page, often looks like a grey box with some input options. The standard post-excerpt is a good example of a meta field.

excerpt meta field

Post Excerpt Metadata Field

You can have as many inputs in each box as you’d like, but when it comes to having a new box, you’re going to need a new function. This is the first part of our custom metadata code because it sets up the naming and creates the function that pulls everything together.

To add this to a custom post type, use the following. Be sure to change the word custom to the identifier of your custom post type.

The Content

Now there’s a place to hold the content, we need a way to input it. This function creates a HTML output of a form that data can be added into. Note that there is no submit button. We’re going to use the default “Publish” or “Update” button to save this information to the database. This will work with both standard posts and custom post types, due to this function being called by the hook in the previous step.

This function does a few things before displaying the inputs. First, we need to be sure that this box is only existing once ( nonce ). Then, we should call any existing metadata. Of course, there won’t be anything yet, but we should pretend and assign a name to the data and save it in a variable. Finally, we can add inputs. Be sure to use the called metadata variable as the starting value, this way anything already saved will be in the input and can easily be edited.


The final step is obviously to save any input that was made. This will use the WordPress save_post hook, which is why we don’t need our own save button. To access this hook, use the following code. Be sure that the names of the fields here are the same as in the content input function.

This function is checking that everything only exists once, that the editor has saving permissions on their account, and then finally saves the data into the specified field. To use this with a custom post type, add the identifier to the hook (replace custom with your id: add_action( 'save_post_custom', 'app_save_meta_boxes_data'); ).

Data Input Options

Of course, you’re not limited to the “text” input that we just created. There are lots of other common input types, and way to display them on the post creation page as well as on the final website.

And that’s it. That’s all there is to know about adding a custom metadata field to a post or custom post type.