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
-
Hi Guido,
I think you should not use dashes in variable names. Try underscores instead in the field name
vsel_multi_dayand within the$_POSTand if it didn’t work let us know.As addition, when to know if a checkbox is checked, you can use
isset( $_POST['name'] )and then update the option to ‘on’ or ‘1’ etc, else delete the option, so that you can use that option to know if the checkbox is checked or not..Samuel.
Hi Samuel,
I hope using the dashes isn’t a problem, because I’m using this in whole form.. and it’s working fine.
Dit not test this already, but this should be better I guess?
<input class="checkbox" id="vsel-multi-day" type="checkbox" name="vsel-multi-day" value="1" <?php if ( isset ( $event_multi_day ) ) checked( $event_multi_day, 1, true ); ?> />As you can see I have a check now..
Guido
This might be cleaner:
// get data same as first post // input field same as first post // update if ( isset( $_POST['vsel-multi-day'] ) ) { update_post_meta( $post_id, 'event-multi-day', sanitize_text_field( $_POST['vsel-multi-day'], '1' ) ); } else { update_post_meta( $post_id, 'event-multi-day', sanitize_text_field( $_POST['vsel-multi-day'], '' ) ); }Any thoughts?
Guido
Hi Guido.
So good if it is working with dashes.
As for the last reply, the value of
$_POST['vsel-multi-day']if set is alwaysonso no need for getting the value and sanitizing etc..Try this:
if ( isset( $_POST['vsel-multi-day'] ) ) { update_post_meta( $post_id, 'event-multi-day', '1' ) ); } else { delete_post_meta( $post_id, 'event-multi-day' ); }If the meta exists (
get_post_meta( $post_id, 'event-multi-day', TRUE )), then it is checked. Else not:$meta = get_post_meta( $post_id, 'event-multi-day', TRUE );$metawill be eitherboolean falseorstring '1'Samuel
Aha, of course. I’ve changed it to this (dit not use the delete_post_meta because my other metabox fields have the same structure):
if ( isset( $_POST['vsel-multi-day'] ) ) { update_post_meta( $post_id, 'event-multi-day', '1' ) ); } else { update_post_meta( $post_id, 'event-multi-day', '0' ) ); }I did another search on the web and found out there are lots of different ways to integrate a checkbox. I like this one.
Guido
Cool!
Glad you fixed it 🙂
The topic ‘Metabox checkbox stays checked’ is closed to new replies.