but remaining values (checkbox, select, images) are not getting saved
I believe those fields are also getting saved, but you are just not displaying them correctly. And you should always escape input/textarea values, e.g. use esc_attr()
for single-line inputs and esc_textarea()
for textareas (multi-line inputs).
So to make your code work correctly, try these:
-
First off, you can define default values for your
your_fields
meta like so, which useswp_parse_args()
:$meta = get_post_meta($post->ID, 'your_fields', true); // Merge with default values, ensuring all keys are set. $meta = wp_parse_args( $meta, array( 'text' => '', 'textarea' => '', 'checkbox' => '', 'select' => '', 'image' => '', ) );
-
Then use the following to display your form fields: (just replace the corresponding field in your existing code)
<input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="<?php echo esc_attr( $meta['text'] ); ?>"> <textarea name="your_fields[textarea]" id="your_fields[textarea]" rows="5" cols="30" style="width:500px;"><?php echo esc_textarea( $meta['textarea'] ); ?></textarea> <input type="checkbox" name="your_fields[checkbox]" value="checkbox" <?php checked( $meta['checkbox'], 'checkbox' ); ?>> <select name="your_fields[select]" id="your_fields[select]"> <option value="">Select..</option> <option value="option-one"<?php selected( $meta['select'], 'option-one' ); ?>>Option One</option> <option value="option-two"<?php selected( $meta['select'], 'option-two' ); ?>>Option Two</option> </select> <input type="text" name="your_fields[image]" id="your_fields[image]" class="meta-image regular-text" value="<?php echo esc_attr( $meta['image'] ); ?>">