WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is kind of a weird one. I'm in the process of building a widget that goes through posts of type product and displays a list of checkboxes in the admin panel to display/show the products in the widgets.

Everything seems to be working fine, except the checked() function is not only adding the checked='checked' but it is also displaying it on the page. Below is the form section of my widget:

function form( $instance ) {
    $instance = wp_parse_args( (array) $instance, array( 'title' => __('Featured Products', 'pp') ) );
    $title = $instance['title'];
    $products = new WP_Query( array( 'post_type' => 'product', 'post_count' => -1 ) );
      <label for="<?php echo $this->get_field_id('title'); ?>">
        <?php _e('Title:', 'mp') ?>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" />

      <?php while ($products->have_posts()) : $products->the_post(); $prod = get_the_id();?>
          $sku = get_post_meta($prod, 'sku', true);
          $sku = $sku[0];
          $$sku = isset($instance[$sku]) ? checked( (bool)$instance[$sku], true ) :false; 

    <input type="checkbox" class="checkbox" <?php echo $$sku; ?> id="<?php echo $this->get_field_id($sku); ?>" name="<?php echo $this->get_field_name($sku); ?>" value="1" />
    <label for="<?php echo $this->get_field_id($sku); ?>"><?php echo _e( the_title() ); ?></label><br />

      <?php endwhile; 

When you go into the widgets meta box in the admin panel the items that are checked and saved display as (Please note that the [] represents the checkbox itself):

checked='checked'[] Product

And the corresponding HTML:

<input type="checkbox" class="checkbox" checked="checked" id="widget-fsol_featured_products_widget-2-WM3987HW" name="widget-featured_product_widget[2][W8DIW2]" value="1">
<label for="widget-featured_product_widget-2-W8DIW2">W8DIW2</label>

I've tried several different 'fixes' with no luck. Anyone have an idea?

share|improve this question
up vote 1 down vote accepted

By default, checked echoes. If you set the (currently empty) third parameter to false, it should work as expected. Change the line to:

$$sku = isset( $instance[$sku] ) ? checked( (bool) $instance[$sku], true, false ) : false;

More info: http://codex.wordpress.org/Function_Reference/checked

share|improve this answer
Wow that was a bonehead move, I wasn't reading the documentation correctly on the subject in regards to the $echo param. Thank you @SeventhSteel! All is well! – voodooGQ Sep 12 '12 at 13:51
I'd vote you up but I'm not quit there yet for Rep. – voodooGQ Sep 12 '12 at 13:51
No problem, glad I could help. – SeventhSteel Sep 12 '12 at 14:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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