Read This Offline With Code With Me APP

Add custom metabox to wordpress post

Posted On: Jun 29, 2015

The default wordpress post or page or any custom post type has only 3 fields like title, description and featured image. If you want to add another field than those available, the you need to add custom metabox. A metabox is any custom field for wordpress post. for eg. You have a page called Reservation, then There should be Title, Description, Featured Image, Reservation Date here Title, Description, Featured Image are available but Reservation Date needs to be created as custom field. it is called Custom Metabox.

Wordpress metabox can be added with

   add_meta_box( $id, $title, $callback, $screen, $context,
         $priority, $callback_args );

Parameters are

$id (string) (required) HTML 'id' attribute of the edit screen section, must be unique, eg. reservation_meta
Default: None

(string) (required) Title of the edit screen section, visible to user
Default: None

(callback) (required) Function that prints out the HTML for the edit screen section. The function name as a string, or, within a class, an array to call one of the class's methods. 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 $metabox item (an array), see Callback args. See the second example under Examples below.
Default: None

(string) (optional) The type of writing screen on which to show the edit screen section (examples include 'post','page','dashboard','link','attachment','custom_post_type' where custom_post_type is the custom post type slug)
Default: null

(string) (optional) The part of the page where the edit screen section should be shown ('normal', 'advanced', or 'side'). (Note that 'side' doesn't exist before 2.7)
Default: 'advanced'

(string) (optional) The priority within the context where the boxes should show ('high', 'core', 'default' or 'low')
Default: 'default'

(array) (optional) Arguments to pass into your callback function. The callback will receive the $post object and whatever parameters are passed through this variable.
Default: null

The following code to add reservation metabox to post

add_action("admin_init", "mytheme_init");
add_action('save_post', 'save_reservation_meta');

function mytheme_init(){
	add_meta_box("reservation_date_metabox", "Reservation Date", "show_reservation_meta", "post", "normal", "high");

function show_reservation_meta(){
	global $post;
	<input id="reservation_date" type="text" value="<?php echo $reservation_date; ?>" name="reservation_date">

function save_reservation_meta()
	global $post;
	if($post->post_type == "post" ) {
		if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
		if(!empty($_POST["reservation_date"])) {
			update_post_meta($post->ID, "_reservation_date", $_POST["reservation_date"]);
		else {			
			delete_post_meta($post->ID, "_reservation_date", '');	


  • custom field
  • metabox
  • wordpress

Share This On