How to add Meta Box to WordPress Theme

Tutorial on how to add custom meta boxes to your WordPress. Add them to posts, pages, and even custom post type pages.

There are a handful of plugins that will help you add custom meta boxes to your WordPress theme. All of them have their pros/cons and at the end of the day it is what you prefer to use.

In this tutorial, I go over how to use the CMB2 plugin to add meta boxes to your WordPress theme. If you are following the video tutorial, the steps below will add meta boxes to your theme.

  1. Install the CMB2 plugin from the WordPress Dashboard
  2. Create a new file in your WordPress theme called meta-boxes.php and save it in the /inc/ directory
  3. In this new meta-boxes.php file, use the example code that CMB2 offers below (you can also find it here):

  4. In your functions.php add the following line of code to call the new meta-boxes.php file

  5. Open one of your Pages from the WordPress Dashboard and verify the meta boxes are displaying and saving the data that you input
  6. Now that we have the data saved in the backend, we need to display the content on the frontend. To do this, add the following code to content-page.php

  7. The last thing I did in the video was change the _yourprefix_ and textdomain of the example meta box code.

The video also mentions data validation when echoing out your meta box value. This is important to understand and study to make sure your data is safe. To read more about data validation, go here.