1

The meta box, fields, etc. are all displaying correctly but when I go to update the page it seems the data in those boxes has not saved. Any ideas? Using a custom post type called 'question'. Would be very grateful in advance for any help! Edit: Also apologies if this is a stupid question, I'm new and a bit confused why this isn't working!

    // Render the custom meta box for the question options
function questionbank_render_options_meta_box( $post ) {
    wp_nonce_field( 'question_options_nonce', 'question_options_nonce' );
    $options = get_post_meta( $post->ID, '_question_options', true );
    if ( ! is_array( $options ) ) {
        $options = array( 'a' => '', 'b' => '', 'c' => '', 'd' => '', 'e' => '', 'correct_answer' => '' );
    }
    if ( ! array_key_exists( 'correct_answer', $options ) ) {
        $options['correct_answer'] = '';
    }
    ?>
    <p>
        <label for="option_a"><?php _e( 'Option A', 'questionbank' ); ?></label><br>
        <input type="text" name="options[a]" id="option_a" value="<?php echo esc_attr( $options['a'] ); ?>">
    </p>
    <p>
        <label for="option_b"><?php _e( 'Option B', 'questionbank' ); ?></label><br>
        <input type="text" name="options[b]" id="option_b" value="<?php echo esc_attr( $options['b'] ); ?>">
    </p>
    <p>
        <label for="option_c"><?php _e( 'Option C', 'questionbank' ); ?></label><br>
        <input type="text" name="options[c]" id="option_c" value="<?php echo esc_attr( $options['c'] ); ?>">
    </p>
    <p>
        <label for="option_d"><?php _e( 'Option D', 'questionbank' ); ?></label><br>
        <input type="text" name="options[d]" id="option_d" value="<?php echo esc_attr( $options['d'] ); ?>">
    </p>
    <p>
        <label for="option_e"><?php _e( 'Option E', 'questionbank' ); ?></label><br>
        <input type="text" name="options[e]" id="option_e" value="<?php echo esc_attr( $options['e'] ); ?>">
    </p>
    <p>
        <label for="correct_answer"><?php _e( 'Correct Answer', 'questionbank' ); ?></label><br>
    <select name="correct_answer" id="correct_answer">
        <option value="a" <?php selected( $options['correct_answer'], 'a' ); ?>><?php _e( 'Option A', 'questionbank' ); ?></option>
        <option value="b" <?php selected( $options['correct_answer'], 'b' ); ?>><?php _e( 'Option B', 'questionbank' ); ?></option>
        <option value="c" <?php selected( $options['correct_answer'], 'c' ); ?>><?php _e( 'Option C', 'questionbank' ); ?></option>
        <option value="d" <?php selected( $options['correct_answer'], 'd' ); ?>><?php _e( 'Option D', 'questionbank' ); ?></option>
        <option value="e" <?php selected( $options['correct_answer'], 'e' ); ?>><?php _e( 'Option E', 'questionbank' ); ?></option>
    </select>
</p>
<?php
// Save the custom meta box data
function questionbank_save_options_meta_box_data( $post_id ) {
    if ( ! isset( $_POST['question_options_nonce'] ) ) {
        return;
    }
    if ( ! wp_verify_nonce( $_POST['question_options_nonce'], 'question_options_nonce' ) ) {
        return;
    }
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    if ( isset( $_POST['post_type'] ) && 'question' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }
    } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    $options = array();
    if ( isset( $_POST['options'] ) && is_array( $_POST['options'] ) ) {
        foreach ( $_POST['options'] as $key => $value ) {
            if ( in_array( $key, array( 'a', 'b', 'c', 'd', 'e', 'correct_answer' ) ) ) {
                $options[ $key ] = sanitize_text_field( $value );
            }
        }
    }
    update_post_meta( $post_id, '_question_options', $options );
}
add_action( 'save_post_question', 'questionbank_save_options_meta_box_data' );
3
  • From what I could tell, your other inputs (question's options) should be saving correctly, but your correct_answer dropdown should use name="options[correct_answer]" instead of just name="correct_answer". Are you sure none of the options are being saved?
    – Sally CJ
    Commented Mar 31, 2023 at 15:17
  • Thanks for that. I've found the problem I think: Was missing a closing brace in this section (now added after the <?php . </select> </p> <?php } // Save the custom meta box data function questionbank_save_options_meta_box_data( $post_id ) {
    – Tom Foster
    Commented Mar 31, 2023 at 15:34
  • 1
    Yes, I noticed that, but I thought it's just a typo in your question. Anyway, you should write an answer and accept it later on.
    – Sally CJ
    Commented Mar 31, 2023 at 15:36

0

Your Answer

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

Browse other questions tagged or ask your own question.