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.
top_level[]
is not an value to compare in checked functino.top_level[]
is an markup'in_array($_POST[top_level])'
and got "syntax error, unexpected 'in_array' (T_STRING), expecting ',' ".$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.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.