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' );
correct_answer
dropdown should usename="options[correct_answer]"
instead of justname="correct_answer"
. Are you sure none of the options are being saved?</select> </p> <?php } // Save the custom meta box data function questionbank_save_options_meta_box_data( $post_id ) {