0

Problem with the nonce field. Trying to figure out why the meta box won't display the saved field after page refresh, i.e. enter code in code field. Hit save. The code just entered does not reappear in the code field. Thank you.

Here is the code I'm using:

$meta_box_code = array(
    'id'       => 'id-meta-box-code',
    'title'    => __( 'code', 'framework' ),
    'page'     => 'post',
    'context'  => 'normal',
    'priority' => 'high',
    'fields'   => array(
        array(
            'name' => __( 'The Code', 'framework' ),
            'desc' => __( 'Place Your Code Here:', 'framework' ),
            'id'   => 'id_code',
            'type' => 'textarea',
        ),
    ),
);

add_action( 'admin_menu', 'add_the_meta_box' );

function add_the_meta_box() {
    global $meta_box_code;

    add_meta_box(
        $meta_box_code['id'],
        $meta_box_code['title'],
        'show_the_meta_box',
        $meta_box_code['page'],
        $meta_box_code['context'],
        $meta_box_code['priority']
    );
}

function show_the_meta_box() {
    global $meta_box_code, $post;

    echo '<input type="hidden" name="id_meta_box_nonce" value="', wp_create_nonce( basename( __file__ ) ), '" />';
    echo '<table class="form-table">';

    foreach ( $meta_box_code['fields'] as $field ) {
        $meta = get_post_meta( $post->ID, $field['id'], true );
        switch ( $field['type'] ) {
            case 'textarea':
                echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th>', '<td>';
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="', '" rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">', '</textarea>';
                break;
        }

    }

    echo '</table>';
}


function id_save_data( $post_id ) {
    global $meta_box_code;

    if ( ! isset( $_POST['id_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['id_meta_box_nonce'], basename( __file__ ) ) ) {
        return $post_id;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        }
    } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }

    foreach ( $meta_box_code['fields'] as $field ) {
        $old = get_post_meta( $post_id, $field['id'], true );
        $new = $_POST[$field['id']];

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
    }

}

add_action( 'save_post', 'id_save_data' );

2 Answers 2

0

There is problem in your show_the_meta_box() function you have used value attribute for the textarea field don't use value attribute for textarea field.

For more information on textarea atrributes visit this page.

So the correct code show_the_meta_box() function is as shown in following.

function show_the_meta_box() {
global $meta_box_code, $post;

echo '<input type="hidden" name="id_meta_box_nonce" value="'. wp_create_nonce( basename( __file__ ) ). '" />';
echo '<table class="form-table">';

foreach ( $meta_box_code['fields'] as $field ) {
    $meta = get_post_meta( $post->ID, $field['id'], true );
    switch ( $field['type'] ) {
        case 'textarea':
            echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th><td>';
            echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '"  rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">'. $meta.'</textarea>';
            break;
    }

}

echo '</table>';
}
1
  • I'm aware the correct way is to use a . I must've missed the comma. Works like a charm! Thank you!
    – user34769
    Jul 1, 2013 at 8:04
0

Firstly, add your nonce field with wp_nonce_field( ):

wp_nonce_field( plugin_basename( __FILE__ ), 'your-nonce-name' );

And secondly, you use , to join your PHP and HTML, this should be ..
For example:

echo '<input type="hidden" name="id_meta_box_nonce" value="', wp_create_nonce( basename( __file__ ) ), '" />';

Should be:

echo '<input type="hidden" name="id_meta_box_nonce" value="' . wp_create_nonce( basename( __file__ ) ) . '" />';
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.