1

I have a 2-level taxonomy terms checkbox list (created with get_categories) that stores values into the usermeta table. I'd like to display the checked items when the page is refreshed.

This is the original code that does not refresh with checkmarks:

$categories = get_categories( $args );

foreach ( $categories as $category ){

echo '<label><input type="checkbox" id="type-'. $category->name . '" value="'. $category->name . '" class="shopping" name="top_level[]"  <?php checked('. $category->name . ', in_array(top_level[]))  ?>>'. $category->name . '</label>';

After reading a number of posts, I came up with this:

<input type="checkbox" id="type-'. $category->name . '" value="'. $category->name . '" class="shopping" name="top_level[]" <?php checked('. $category->name . ', top_level[])  ?>>

The only thing this does is to display an extra > in front of the term. Inspecting the element in Firebug shows this:

<input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php= checked(fashion, top_level[]) ?="">

If I edit the HTML in Firebug, it shows:

<input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php="" checked(fashion,="" top_level[])="" ?="">

Tried adding in_array:

<input type="checkbox" id="type-'. $category->name . '" value="'. $category->name . '" class="shopping" name="top_level[]"  <?php checked('. $category->name . ', in_array(top_level[]))  ?>>

Again, Firebug just shows the PHP code.

What am I doing wrong? How do I compare the value to something in the array?

Thanks in advance!

UPDATE check marks are still not showing after user profile is updated.

var_dump shows the proper values for $current array, the values are serialized in the db.

This is my latest attempt:

$categories = get_categories( $args );
$current = get_user_meta($user->ID, 'top_level', false);    
$currentsel = maybe_unserialize( $current );

foreach ( $categories as $category ){
    ?>
    <label><input type="checkbox" id="type-<?php echo $category->name; ?>" value="<?php echo $category->name; ?>" class="shopping" name="top_level[]"  <?php if(in_array($category->name, $currentsel)){ echo 'checked="checked"';}  ?>><?php echo $category->name; ?> </label>
    <?php

I've also just come across this, although the article is 6 years old and I'm not sure if the checked function still works this way:

<p>
<input class="checkbox" type="checkbox" <?php checked( (bool) $instance['do_something'], true ); ?> id="<?php echo $this->get_field_id( 'do_something' ); ?>" name="<?php echo $this->get_field_name( 'do_something' ); ?>" />
<label for="<?php echo $this->get_field_id( 'do_something' ); ?>"><?php _e('Display the Do Something action?'); ?></label>

The article says that "the key is the (bool) in the checked() function. If it does not exist the checkbox will not maintain the visual check mark correctly."

Tried it with this:

<label><input type="checkbox" id="type-<?php echo $category->name; ?>" value="<?php echo $category->name; ?>" class="shopping" name="top_level[]"  <?php checked((bool)$category->name, $current);  ?>><?php echo $category->name; ?> </label>

I'm at a total loss as to what to try next.

5
  • It looks like there is an confusion. First try to figure out where do you save those value and try to compare it with the categories. top_level[] is not an value to compare in checked functino. top_level[] is an markup
    – WPTC-Troop
    Nov 19 '15 at 7:02
  • The value is saved in $_POST[top_level]. In another post, it was suggested to use the array key with [ ] within the checked function. In another post, it was recommended to use name="var_name[]" for the select attribute. Isn't that what I should be comparing to in the checked function?
    – 1td
    Nov 19 '15 at 18:27
  • Also, I just tried this 'in_array($_POST[top_level])' and got "syntax error, unexpected 'in_array' (T_STRING), expecting ',' ".
    – 1td
    Nov 19 '15 at 18:28
  • Following the example in the codex, I added $current = get_user_meta($user_id, 'continent', $true); then used this for the checked function <?php checked('. $category->name . ', $current, false); ?>. Firebug still shows the function verbatim rather than the result of the function. I also realized I forgot the semi-colon. Added that in.
    – 1td
    Nov 19 '15 at 18:41
  • Tried echo <<<EOD '<label><input type="checkbox" id="type-'. $category->name . '" value="'. $category->name . '" class="shopping" name="top_level[]" <?php checked('. $category->name . ', $current, false); ?>>'. $category->name . '</label>'; EOD; making sure to follow the herdoc specs. Now all my checkbox fields show all kinds of quotes and commas.
    – 1td
    Nov 19 '15 at 18:53
1

OK Try the below

    $current = get_user_meta($user_id, 'continent', $true);
    $categories = get_categories( $args );

    foreach ( $categories as $category ){
?>
    <label><input type="checkbox" id="type-<?php echo $category->name; ?>" value="<?php echo $category->name; ?>" class="shopping" name="top_level[]"  <?php checked($category->name, $current);  ?>><?php echo $category->name; ?> </label>
  <?php  
    }

If $current = get_user_meta($user_id, 'continent', $true); value is an array then try the below UPDATED for array

    foreach ( $categories as $category ){
?>
    <label><input type="checkbox" id="type-<?php echo $category->name; ?>" value="<?php echo $category->name; ?>" class="shopping" name="top_level[]"  <?php if(in_array($category->name, $current)){ echo 'checked="checked"';}  ?>><?php echo $category->name; ?> </label>
  <?php  
    }

From your comment I see you used this <?php checked('. $category->name . ', $current, false); ?> false shouldn't be there as it won't echo the checked string instead it return the checked string.

12
  • Tried your suggestion. The field shows: name, $current) ?>>Fashion Firebug: <input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php="" checked($category-=""> Put in <?php checked('. $category->name . ', $current) ?> Firebug shows <input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php checked(fashion, $current) ?>
    – 1td
    Nov 19 '15 at 19:34
  • Edit HTML and it shows <input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]" <?php="" checked(fashion,="" $current)="" ?=""> Where are all these ="" coming from and could they be the reason it's not working?
    – 1td
    Nov 19 '15 at 19:34
  • Note, the database is definitely updating correctly when submitting the form, so the value for top_level is there.
    – 1td
    Nov 19 '15 at 19:36
  • ah am really sorry, there was an error in answer. Please check the updated answer now. I just used markup plus php instead of echo all.
    – WPTC-Troop
    Nov 19 '15 at 19:41
  • Tried your correction. HTML output: <input type="checkbox" id="type-Fashion" value="Fashion" class="shopping" name="top_level[]">. No "checked" attribute. But the db updates correctly.
    – 1td
    Nov 19 '15 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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