Posted in WordPress

Custom meta box in WordPress

Custom meta box in WordPress Posted on December 6, 2015Leave a comment

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:

  • $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:

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.

Remove meta box

To remove an existed meta box, use this function:

  • $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:

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


Leave a Reply

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