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.
- Install the CMB2 plugin from the WordPress Dashboard
- Create a new file in your WordPress theme called
meta-boxes.phpand save it in the
- In this new
meta-boxes.phpfile, use the example code that CMB2 offers below (you can also find it here):
- In your functions.php add the following line of code to call the new meta-boxes.php file
- Open one of your Pages from the WordPress Dashboard and verify the meta boxes are displaying and saving the data that you input
- 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
- 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.