WordPress supports us several of meta boxes such as Categories
, Tags
, Excerpt
, Discussion
, Author
, .. but sometimes we have to create, remove or modify meta boxes for specific reasons.
Create meta box
This function helps us to create a meta box:
1 |
add_meta_box( $id, $title, $callback, $screen, $context, $priority, $callback_args ); |
$id
: (required) HTML ‘id’ attribute of the edit screen section.$title
: (required) Title of the edit screen section, visible to user.$callback
: (required) Function that prints out the HTML for the edit screen section. The callback can accept up to two arguments: the first argument is the $post object for the post or page that is currently being edited. The second argument is the full meta box item (an array).$screen
: (optional) The type of writing screen on which to show the edit screen section ( eg: ‘post’, ‘page’, .. or our custom post type.$context
: (optional) The part of the page where the edit screen section should be shown ( eg: ‘normal’, ‘advanced’, or ‘side’ ).$priority
: (optional) The priority within the context where the boxes should show ( eg: ‘high’, ‘core’, ‘default’ or ‘low’).$callback_args
: (array) (optional) Arguments to pass into your callback function. The callback will receive the$post
object and whatever parameters are passed through this variable.
Here is an example for create new meta box:
1 2 3 4 5 6 7 8 9 10 11 12 |
function custom_meta_box() { add_meta_box( 'custom_metabox', 'Custom Metabox', 'custom_meta_box_content', 'post', 'side', 'high' ); } add_action( 'add_meta_boxes', 'custom_meta_box' ); function custom_meta_box_content() { echo 'Hello meta box'; //include 'path/file.php'; } |
In $callback
function, you can write HTML elements into it or include
or require
particular files.
Tips: $callback
function accepts two arguments $post
, $args
, you can use them to make your function to be more dynamic
. For example, with 1 callback function I can use for different meta boxes, it saves me a lot of lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function add_meta_boxes() { add_meta_box( 'metabox_1', 'Meta box 1', 'include_file', 'post', 'normal' ); add_meta_box( 'metabox_2', 'Meta box 2', 'include_file', 'page', 'side' ); add_meta_box( 'metabox_3', 'Meta box 3', 'include_file', 'post', 'side' ); add_meta_box( 'metabox_4', 'Meta box 4', 'include_file', 'post', 'normal' ); } function include_file( $post, $args ) { // $args['id'] is also a file name include_once 'my_path' . $args['id'] . '.php'; } |
Remove meta box
To remove an existed meta box, use this function:
1 |
remove_meta_box( $id, $page, $context ); |
$id
: (required) see at Create meta box. Some of the available id values are given below:
authordiv
– Author metabox
categorydiv
– Categories metabox.
commentstatusdiv
– Comments status metabox (discussion)
commentsdiv
– Comments metabox
formatdiv
– Formats metabox
pageparentdiv
– Attributes metabox
postcustom
– Custom fields metabox
postexcerpt
– Excerpt metabox
postimagediv
– Featured image metabox
revisionsdiv
– Revisions metabox
slugdiv
– Slug metabox
submitdiv
– Date, status, and update/save metabox
tagsdiv-post_tag
– Tags metabox
{$tax-name}div
– Hierarchical custom taxonomies metabox
trackbacksdiv
– Trackbacks metabox$page
: see at Create meta box$context
: see at Create meta box
Here is an example to remove meta box:
1 2 3 4 5 6 |
function custom_meta_box() { remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'normal' ); } add_action( 'admin_menu' , 'custom_meta_box' ); |
Modify meta box
We can modify meta box, such as edit $title
, move it to another location. To do this, we have to do this trick:
- Remove a meta box that we want to move.
- Add it again.
This example will show you how to do that, we will remove tags
meta box, change its name and change $context
to normal
, $priority
to core
1 2 3 4 5 6 7 8 |
function custom_meta_box() { global $wp_meta_boxes; unset( $wp_meta_boxes['post']['side']['core']['tagsdiv-post_tag'] ); add_meta_box( 'tagsdiv-post_tag', 'New title', 'post_tags_meta_box', 'post', 'normal', 'core', array( 'taxonomy' => 'post_tag' )); } add_action( 'add_meta_boxes', 'custom_meta_box' ); |