How To Add Metabox In WordPress

Metaboxes are one of the finest features that WordPress provides. You can get the data from the post / page creator by the Metabox as like you get in the WordPress editor.

Here, with the metaboxes the page creator can easily add the required data and it could be displayed anywhere on the page using the template of the page. Here in this tutorial, we will discuss on adding a metabox to a page. Before that let’s understand what exactly metaboxes are?

A metabox is a draggable input box where the page creator can easily put in the data. These data could be displayed on the page anywhere using the template. In metabox  two types of data that could be included:

  1. Metadata (Custom Fields)
  2. Taxonomy terms

Let’s see how the metabox can be added in the WordPress page’s editor. Here we will create a box where the editor can add some text to be displayed on the page.

Create a new file named as page-metabox.php and add the below code there.

  1. <?php
  2. //Lead Text Metabox
  3. function page_lead_text_metabox(){
  4. 	add_meta_box(
  5. 			'page_lead_text', //Unique ID
  6. 			esc_html__( 'Page Lead Text', 'I11L' ), //Title
  7. 			'page_lead_text_caption_metaboxes', //Callback function
  8. 			'page', //for pages
  9. 			'normal', //Context
  10. 			'default' //priority
  11. 			);
  12. }
  13.  
  14. function page_lead_text_caption_metaboxes( $object, $box ) {
  15.  
  16. 	wp_nonce_field( basename( __FILE__ ), 'page_lead_text_caption_metaboxes' );
  17.  
  18. 	$lead_text 	= esc_attr( get_post_meta( $object->ID, 'page_lead_text', true ) );
  19.  
  20. 	?>
  21. 	<p>Page Lead Text</p>
  22. 		<input type="text" class="regular-text" name="page_lead_text" value="<?php echo $lead_text; ?>" placeholder="Page Lead Text">		
  23. 	<?php 
  24. }

Here the function page_lead_text_metabox() will call the function page_lead_text_caption_metaboxes() function and the HTML here in this functionn will put up a metabox in the page editing page.

Now, we need to save the metabox once you update or save the page. The below mentioned code snippet will save the metabox data.

  1. /* Save the meta box's post metadata. */
  2. function save_page_lead_text_meta( $post_id, $post ) {
  3.  
  4. 	/* Verify the nonce before proceeding. */
  5. 	if ( !isset( $_POST['page_lead_text_caption_metaboxes'] ) || !wp_verify_nonce( $_POST['page_lead_text_caption_metaboxes'], basename( __FILE__ ) ) )
  6. 		return $post_id;
  7.  
  8. 	/* Get the post type object. */
  9. 	$post_type = get_post_type_object( $post->post_type );
  10.  
  11. 	/* Check if the current user has permission to edit the post. */
  12. 	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
  13. 		return $post_id;
  14.  
  15. 		$field_names = array('page_lead_text',);
  16.  
  17. 		foreach($field_names as $field_name)
  18. 		{
  19. 			/* Get the posted data and sanitize it for use as an HTML class. */
  20. 			$new_meta_value = ( isset( $_POST["$field_name"] ) ? $_POST["$field_name"] : '' );
  21.  
  22. 			/* Get the meta key. */
  23. 			$meta_key = $field_name;
  24.  
  25. 			/* Get the meta value of the custom field key. */
  26. 			$meta_value = get_post_meta( $post_id, $meta_key, true );
  27.  
  28. 			/* If a new meta value was added and there was no previous value, add it. */
  29. 			if ( $new_meta_value && '' == $meta_value )
  30. 			add_post_meta( $post_id, $meta_key, $new_meta_value, true );
  31.  
  32. 			/* If the new meta value does not match the old value, update it. */
  33. 			elseif ( $new_meta_value && $new_meta_value != $meta_value )
  34. 			update_post_meta( $post_id, $meta_key, $new_meta_value );
  35.  
  36. 			/* If there is no new meta value but an old value exists, delete it. */
  37. 			elseif ( '' == $new_meta_value && $meta_value )
  38. 			delete_post_meta( $post_id, $meta_key, $meta_value );		
  39. 		}	
  40.  
  41. }

This above code snippet will save the metabox’s data into the database. It will update the data in case the data already existed and if not, it will save the data.

Now, we need to trigger the page_lead_text_metabox() function whenever the page editor is opened. You need to add the hooks of add_meta_boxes and save_post as mentioned below.

Add the below given code snippet into your theme’s functions.php.

  1. include(dirname(__FILE__) .'/page-meatbox.php');
  2.  
  3. add_action( 'add_meta_boxes', 'page_lead_text_metabox' );
  4.  
  5. add_action( 'save_post', 'save_page_lead_text_meta', 10, 2 );

This completes the tutorial to add metabox in WordPress pages and posts.

Related Post

One thought on “How To Add Metabox In WordPress

  1. Simple but effective – the best kind of coding. Very easy to understand. Saved a lot of time.

    Thanks,

Comments are closed.