1

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:

  1. 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
  2. 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?

1
  • This isn't a "problem". It is how forms work. How you deal with it depends on the effects/logic that you want, but that is HTML and PHP not WordPress.
    – s_ha_dum
    Commented Dec 16, 2015 at 17:12

1 Answer 1

1

I managed to crack this nut after few days of experimenting.

<!-- Note how name attribute has actual number in brackets -->

<input type="checkbox" name="feature[1]" id="feature-one" value="feature_one"<?php if ( $feature_one ) { echo 'checked'; } ?> />
<input type="checkbox" name="feature[2]" id="feature-one" value="feature_one" <?php if ( $feature_one ) { echo 'checked'; } ?> />

<?php

//This happens on form submit or when you press edit button
//Number or checkboxes goes here which eventually become keys
$checkboxes = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);

$features = array();

$p = array_key_exists('feature', $_POST) ? $_POST['feature'] : array();

foreach( $checkboxes as $i ) {
    if(array_key_exists( $i, $p ) ) {
        //If is checked
        $features[$i] = 1;
    }
    else {
        //If is not checked
        $features[$i] = 0;
    }
}
//Save to database as an array where checked are 1 and unchecked are 0
update_term_meta( $term_id, 'features', $features );

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.