How to add metaboxes for custom post types – WordPress

Here In this article, You will know how to add metaboxes for Custom posts in write screen inside WordPress admin section. WordPress provides some functions to add a section where you can manage your additional content of post.

In this example, I am adding a metabox as name “Images” under my custom post (lets consider post type is “my-images“). Following screen shows you how metaboxes appear after adding.

 

Lets start to write code as below:

Displaying Meta Boxes with the ‘add_meta_box’ Function

The add_meta_box function is the primary function that allows for information to be added to the add/edit screens for WordPress post types. This powerful function is easy to leverage and can lead to customized content being added to WordPress write screens.
The add_meta_box function takes seven arguments. The arguments with each argument’s data type, required/optional status, and default value are listed below.

Argument Data Type Required Optional Default
$id string X No default
$title string X No default
$callback string X No default
$page string X No default
$context string X advanced
$priority string X required
$callback_args array X null

 

Add following step-wise code in your functions.php file.

Step-1:  Use function 'add_meta_boxes' :

add_action( 'add_meta_boxes', 'add_custom_metaboxes');

Here first parameter is WordPress hook(Do not change it) and second parameter is custom function name which is define in next step below:

Step-2: Define callback function ‘add_custom_metaboxes‘:

function add_custom_metaboxes(){
   add_meta_box( '$id', '$metabox_title', '$callback_function', '$post_type', '$metabox_position','$metabox_display_priority' );
}

You can read the full parameters for add_meta_box in the codex. I also listed them here:

  • $id (string) (Required):  the html id that will be applied to this metabox.
  • $metabox_title (string) (Required) : This is metabox title that appears at the top of the new metabox when displayed.
  • $callback_function (callable) (Required): This is a function which will load the html/content into the metabox. In this function you can write anythings which needs to show inside metabox.
  • $post_type (string|array|WP_Screen) (Optional): Here you have to put your custom post type (for this exaple we considered “my_images” post type).
  • $metabox_position (string) (Optional): Here you can tell to WordPress where my metabox will appear in screen. If you wanted it to load below the content area, you could put “normal”. Possible values are: ‘normal‘, ‘side‘, and ‘advanced‘.
  • $metabox_display_priority (string) (Optional): This controls where the metabox will display in relation to the other metaboxes. You can put “high”, “low” or “default”.

Step-3: Define your callback function where you need to add HTML/Content for your metabox.

function callback_function($post, $metabox){
    global $post;
   // put your content here
   // in $post you will get post detail
}

That’s all for adding metaboxes for custom post type screen.

Note:  In case you need to add some input fields in metabox and later want to save these fields at time of publish post then you must check article: How to Save metabox fields when publish post ??

 

By Ashutosh Pratap

I am software developer by profession and doing work and research in field of Computer programmings like PHP, WordPress, Magento, jQuery, Google APIs and many more web languages. Apart from this I write blogs on programming languages like PHP, Javascript, WordPress etc. I am also individual and independent Freelancer so you can hire me for your web work.

Leave a comment

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