Im trying out new term meta feature introduced in 4.4.
I've got plenty of checkboxes which represents features in custom taxonomy edit screen.
My current setup (note that Im using correct hooks, I just striped them away to make code simpler):
<?php
//Get previous values from db - this is edit page afterall
$feature_one = get_term_meta( $term->term_id, 'feature_one', true );
$feature_two = get_term_meta( $term->term_id, 'feature_two', true );
$feature_three = get_term_meta( $term->term_id, 'feature_three', true );
//Imagine that there's over 20 of these
?>
<input type="checkbox" name="feature[]" id="feature-one" value="feature_one" <?php if ( $feature_one ) { echo 'checked'; } ?> />
<input type="checkbox" name="feature[]" id="feature-two" value="feature_two" <?php if ( $feature_two ) { echo 'checked'; } ?> />
<input type="checkbox" name="feature[]" id="feature-three" value="feature_three" <?php if ( $feature_three ) { echo 'checked'; } ?> />
<!-- Also over 20 of these -->
<?php
//Triggers if taxonomy edit button is pressed
foreach( $_POST['feature'] as $feature ) {
if( isset( $feature ) && '' !== $feature ) {
$checked_feature = $feature;
update_term_meta( $term_id, $checked_feature, 1 );
}
}
/* This outputs in database like this if all 3 are checked
meta_key meta_value
____________________________________
feature_one | 1
feature_two | 1
feature_three | 1
*/
/* How I need to output it in frontend - V is checked icon, X is unchecked
|
v
V features one //have specific feature
X feature two //VERY IMPORTANT to also show features that don't apply
V feature three //have specific feature
*/
?>
Problems:
- PHP doesn't return anything if checkbox is not checked. If feature was checked previously and it's not checked if edited again later, value is not updated in database
- Imagine over 20 checkboxes.. Im having hard time to save these to array with specific order so that labels and values are paired correctly in front-end, also this would not be a problem if unchecked checkboxes returned 0 or something and I could make an array where checked are 1, unchecked 0
How do you solve this "unchecked checkboxes doesn't return anything" problem?