Tuesday , 16 July 2019
Home / Programming / WordPress / Plugins / WordPress Meta Boxes – WordPress Plugin Tutorial
WordPress Meta Boxes
WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

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

WordPress meta boxes are used to display certain information or options in a page. It’s when you have something to display and  don’t want it to  have a page of its own, but would like it to be a part of a page. An example of a  meta boxes is  the ones you see on the WordPress admin dashboard. Let’s create a meta box.

WordPress Meta Boxes

Create a new plugin called wpMetaBoxes and open up your index.php file. Let’s update it to this.

We’re hooking into the add_meta_boxes action. This action is triggered when WordPress starts adding meta boxes to the page. We must hook into this before we actually create a meta box because we only want to create meta boxes  when WordPress allows it.  We also include a file called func.inc.php. This file will contain our function we called for in our action hook.

To actually create a meta box, we use the add_meta_box WordPress function. This function has 7 parameters. The first 4 are required. The last 3 are optional.

  • ID – The ID given to our WordPress meta boxes. This will be used in the DIV tag wrapped around our WordPress meta boxes.
  • Title – This is a simple name for your WordPress meta box. This is displayed to your user when they see your meta box.
  • Callback  – The function that will run when this meta box is added. This function will display the contents of our meta box.
  • Post Type – Where should this meta box be displayed. You don’t use a page name, but the post types instead. Examples of this would be post, page, link, attachment and custom post types.
  • Context – What part of the page should this meta box be displayed by default. ('normal''advanced', or 'side')
  • Priority – This is when the meta box should be added. First or last?
  • Callback Args – Arguments to be passed into your callback function defined earlier.

We haven’t talked about post types yet, but just know that it’s what kind of content you’re using. Post is the most common post type. It’s what you see when you are creating or editing a post. In our case, we’re adding our meta box to the post type of post. You’ll see what I mean in a second. The priority is also different from what we’re use to. Instead of numbers, it uses high, core, default and low. Before we activate our plugin, we must first create the callback function we called in our add_meta_box function.

Add this function after the jk_mb_create function.

Here, we’re echoing out ‘This a custom metabox!’. We can echo out anything we want. This will be displayed to our user. Let’s save our file and then activate our plugin. You should get what you see below when you try to add a new post.

WordPress Meta Boxes
WordPress Meta Boxes


Creating WordPress meta boxes can be very useful to your plugins. It’s best that you use meta boxes instead of creating your own because it’s consistent. Your plugins looks should match the theme of the WordPress admin dashboard as much as possible. If you would like to learn more about meta boxes, click here.

About Jasko Koyn

Check Also

WordPress Widgets API

Widgets API – WordPress Plugin Tutorial

The widgets API is what we’ll be learning next. It’s very simple to use. It …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.