Metabox checkbox stays checked
-
Hi!
I have added a checkbox to my metabox:
// get data $event_multi_day = get_post_meta( $post->ID, 'event-multi-day', true ); // input field <input class="checkbox" id="vsel-multi-day" type="checkbox" name="vsel-multi-day" value="1" <?php checked( $event_multi_day, 1, true ); ?> /> // save data if ( isset( $_POST['vsel-multi-day'] ) ) { update_post_meta( $post_id, 'event-multi-day', sanitize_text_field( $_POST['vsel-multi-day'] ) ); }
As you can see I’m using the checked feature described here.
After pressing submit, the checkbox stays checked!
After searching the net I’ve added this to the update feature:
// delete value if not set if ( !isset( $_POST['vsel-multi-day'] ) ) { delete_post_meta($post_id, 'event-multi-day'); }
Because solution above isn’t described in codex I’m wondering whether this is the common solution for a checkbox?
Guido
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Metabox checkbox stays checked’ is closed to new replies.