Title: Custom Meta
Last modified: August 30, 2016

---

# Custom Meta

 *  [czone](https://wordpress.org/support/users/czone/)
 * (@czone)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/custom-meta-3/)
 * Hello friends,
    I am having trouble in adding Custom Meta boxes. I want to add
   few special meta boxes for Pages only. Here is my code and i dont know where 
   i made the mistake but its not saving.
 *     ```
       add_action( 'add_meta_boxes', 'page_heading_box' );
       function page_heading_box()
       {
           add_meta_box( 'page_heading', 'Add Page Heading', 'page_heading', 'page', 'normal', 'high' );
       }
   
       function page_heading( $post )
       {
       	$values = get_post_custom( $post->ID );
       	$text = isset( $values['page_heading'] );
           ?>
               <p>
               	<label for="page_heading" class="custom_lbl_full">Page Heading</label>
                   <input type="text" class="widefat custom_input" name="page_heading" id="page_heading" value="<?php echo $text; ?>" />
               </p>
               <?php
       }
   
       add_action( 'save_post', 'cd_meta_box_save' );
       function cd_meta_box_save( $post_id )
       {
           if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
           if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
           if( !current_user_can( 'edit_post' ) ) return;
   
           // Make sure your data is set before trying to save it
           if( isset( $_POST['page_heading'] ) )
               update_post_meta( $post_id, 'page_heading', wp_kses( $_POST['page_heading'] ) );
       }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/custom-meta-3/#post-6436468)
 * Hi czone
 * You’ll have to add a nonce to your metabox.
    [https://codex.wordpress.org/Function_Reference/wp_nonce_field](https://codex.wordpress.org/Function_Reference/wp_nonce_field)
   [https://codex.wordpress.org/Function_Reference/add_meta_box](https://codex.wordpress.org/Function_Reference/add_meta_box)
 * Try it with this:
 *     ```
       add_action( 'add_meta_boxes', 'page_heading_box' );
       function page_heading_box() {
       	add_meta_box( 'page_heading', 'Add Page Heading', 'page_heading', 'page', 'normal', 'high' );
       }
   
       function page_heading( $post ) {
       	$page_heading = get_post_meta( $post->ID, 'page_heading', true );
       	$text = !empty( $page_heading ) ? $page_heading : '';
   
       	// Add a nonce field so we can check for it later.
       	wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
       ?>
               <p>
               	<label for="page_heading" class="custom_lbl_full">Page Heading</label>
                   <input type="text" class="widefat custom_input" name="page_heading" id="page_heading" value="<?php echo $page_heading; ?>" />
               </p>
               <?php
       }
   
       add_action( 'save_post', 'cd_meta_box_save' );
       function cd_meta_box_save( $post_id ) {
       	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
       	if ( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
       	if ( !current_user_can( 'edit_post' ) ) return;
   
       	// Make sure your data is set before trying to save it
       	if ( isset( $_POST['page_heading'] ) &&  $_POST['page_heading'] ) {
       		update_post_meta( $post_id, 'page_heading', wp_kses( $_POST['page_heading'] ) );
       	}
       }
       ```
   
 *  Thread Starter [czone](https://wordpress.org/support/users/czone/)
 * (@czone)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/custom-meta-3/#post-6436544)
 * Thanks Friend .. it worked but can u tell me how to add more than one field in
   same box? like I want to have a box name Heading with 2 input fields one for 
   heading and one for sub heading.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Meta’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [czone](https://wordpress.org/support/users/czone/)
 * Last activity: [10 years, 9 months ago](https://wordpress.org/support/topic/custom-meta-3/#post-6436544)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
