• 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

    (@keesiemeijer)

    Hi czone

    You’ll have to add a nonce to your metabox.
    https://codex.www.ads-software.com/Function_Reference/wp_nonce_field
    https://codex.www.ads-software.com/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

    (@czone)

    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.