Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

all

Now i create a widget plugin, here i have problem the checkbox problem, dont have save the value, then i search form Net, and found the answer, but now i dont have understand the means of (bool).

        <label for="<?php echo $this->get_field_id( 'post_date' ); ?>">
    <input class="checkbox" type="checkbox" name="<?php echo $this->get_field_name( 'post_date' ); ?>" id="<?php echo $this->get_field_id( 'post_date' ); ?>"  <?php checked( (bool) $instance['post_date'], true ); ?> />
    <?php _e( 'Show post date' ); ?>
  </label>
share|improve this question
add comment

1 Answer

I also got this problem before. I try to use the (bool) but the problem still occured.

(bool) $instance['post_date']

will always return true unless $instance['post_date'] is an empty array, NULL or empty string

you can check this from http://php.net/manual/en/language.types.boolean.php

<?php
    var_dump((bool) "");        // bool(false)
    var_dump((bool) 1);         // bool(true)
    var_dump((bool) -2);        // bool(true)
    var_dump((bool) "foo");     // bool(true)
    var_dump((bool) 2.3e5);     // bool(true)
    var_dump((bool) array(12)); // bool(true)
    var_dump((bool) array());   // bool(false)
    var_dump((bool) "false");   // bool(true)
?>

so to handle this problem, I print the $instance['post_date'] and get the value 'on'. So now my code currently look something like this and working perfectly.

<?php checked( $instance['post_date'], 'on' ); ?>
share|improve this answer
    
Upvote for 'on' doing the trick! –  goto10 Sep 22 '11 at 16:44
add comment

Your Answer

 
discard

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.