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?

1 Answer 1


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

  • 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, 2012 at 13:51
  • I'd vote you up but I'm not quit there yet for Rep.
    – voodooGQ
    Sep 12, 2012 at 13:51
  • No problem, glad I could help. Sep 12, 2012 at 14:25

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.