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.
* Plugin Name: WP Meta Boxes
* Plugin URI: http://jaskokoyn.com
* Description: A Basic WordPress to show how meta boxes work.
* Author: Jasko Koyn
* Version: 1.0
* Author URI: http://jaskokoyn.com
// Set up our WordPress Plugin
if ( version_compare( get_bloginfo( 'version' ), '3.0', '<' ) )
wp_die( "You must update WordPress to use this plugin!" );
register_activation_hook( __FILE__, 'jk_check_WP_ver' );
// Include or Require any files
include( 'inc/func.inc.php' );
// Action & Filter Hooks
add_action( 'add_meta_boxes', 'jk_mb_create' );
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.
add_meta_box( 'jk_custom_metabox_id', 'Custom Meta Box', 'jk_mb_message', 'post', 'normal', 'high' );
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.
echo 'This is a custom metabox!';
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.
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.