This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Let’s finish up our plugin by extending it with metadata. So far, we’re able specify a post type and add taxonomy to it, but what about adding some custom fields? This can easily be accomplished with metadata. It’s very simple to work with WordPress metadata. We’ll be creating meta boxes again, so if you forgot how to create meta boxes, then go back and learn how to do so in our previous tutorials.

WordPress MetaData

We’ll be working on the recipe plugin we created in the last 2 tutorials. Open it up and add this line of code under the hook section in our index.php file.

1

add_action('add_meta_boxes','jk_create_recipe_ingredient_mb');

We’ll be using the add_meta_boxesaction hook. Let’s define our jk_create_recipe_ingredient_mb() function in the recipe-post-type.php file.

PHP

1

2

3

4

5

6

7

8

9

functionjk_create_recipe_ingredient_mb()

{

add_meta_box('jk_recipe_ingredients_id',

'Recipe Ingredients',

'jk_mb_recipe_ingredient_display',

'recipes',

'normal',

'high');

}

Nothing new here, we’re just creating a meta box. Two things to take from this. The 4th parameter accepts the page or post type this meta box will show up on. In our case, this would be recipes. The next thing is the first parameter which is the ID. It’s very important you remember this later on.

All we’re doing is creating a simple form here. If we did everything right, then you should be able to see the meta box when you try to edit or add a new recipe. The next step is to update the data and display the data.

Under our hook section in the index.php file, add this action hook.

PHP

1

add_action('save_post','jk_mb_recipe_ingredients_update');

We’re introduced to a next action hook called save_post. This hook is triggered whenever you try to save post data. Let’s create the jk_mb_recipe_ingredients_update()function.

We’re passed in a variable named $id which is unique to our post. This is automatically given to us by WordPress. This is the ID to our meta data. Since anyone can hook into this action, we have to make sure we’re only receiving and updating our form. So, we do a simple issetcheck. If all is good, then we use the update_post_meta() function to update the meta data.

Usually we have to create the post meta before we can update it, but the add_meta_box() function actually creates the meta data for us by using the ID in the first parameter. The update_meta_box() function has 3 parameters. The first parameter is the ID of the meta data. We use the ID provided by WordPress. The next parameter takes in the name of the meta data we’re updating. In our case, it’s jk_recipe_ingredients_id. This value corresponds to the value in the first parameter of the add_meta_box() function. Lastly, we pass in the value.

Now that we’ve updated the meta, it’s time to display the metadata. Let’s go back to our jk_mb_recipe_ingredient_display()function and update it to this.

Three things to take from this. You’ll notice that we’re passed a variable named $post. This is provided by WordPress to give us some information about the post. We’ll need it for it’s ID. We then create a variable named $ingredients which will store our meta data. The get_post_meta() function grabs our post meta data. The first parameter accepts the ID of the meta data. In our case, this is stored in the $post variable. Next is the name of the meta data. Lastly, we do have the choice of making our data an array or a single variable. Since we’re only working with 1 value, we input true. If we set this to false, then this function would return an array.

The last thing we do is then echo out this variable in the input field. That’s it! Try creating or editing a recipe. You have successfully created a meta box with meta data. Here’s what it should look like.

WordPress Metadata

Conclusion

Utilizing WordPress metadata can make your plugins work with post types easy. This concludes this 3 part series on working with WordPress post types. If you would like to learn more about WordPress meta data then click here.