In the Public Function Update this code is sitting →

`public function update($new_instance, $old_instance)

      $instance['description_box'] = $new_instance['description_box'];

In the public function widget, this code is sitting →

public function widget($args, $instance) {

    $description_box = $instance[ 'description_box' ] ? 'true' : 'false';

In the public function→

public function form( $instance ) {

This code is sitting →

<p> <input class="checkbox" type="checkbox" <?php checked( $instance[ 'description_box' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'description_box' ); ?>" name="<?php echo $this->get_field_name( 'description_box' ); ?>" />
        <label for="<?php echo $this->get_field_id( 'description_box' ); ?>">Check whether to display description or not</label>

Now what i want is when the condition is true i.e. the box is checked then execute some code.

If ($description_box is true) {
<p>some text</p>


How to correctly write this using this →https://codex.wordpress.org/Function_Reference/checked

Update →

I tried using this →

<?php if($description_box==0){ ?>
                                    <?php } ?>

But the problem is if the widget is in 2 different sidebars, in one checkbox ticked and in another not, but the result is same in both i.e. it either displays <p></p> or not based on the either $description_box is set to 0/1 or true/false in the php if condition. whats the remedy?

  • What's the actual problem here? "it either displays <p></p> or not based on the either $description_box is set to 0/1 or true/false in the php if condition" sounds like it's doing what you want. Commented Aug 4, 2017 at 3:01
  • Widgets go to different sidebars? I have two sidebars and when I drag the widget in these two sidebars: In one widget, which is placed in sidebar1 I ticked the check box, and in another widget(same widget), which is placed in the sidebar 2 I didn't check the check box, but both the widget(same widgets) in 2 different sidebars have the same behaviour either they are displaying the <p></p> or not at the same time, but the truth is that in one widget-sidebar arrangement we have opted to check the check box, and in another we didn't. Did you get my Point now?
    – WordCent
    Commented Aug 4, 2017 at 6:50
  • Where are you setting the $description_check_box variable, before you compare it? This is the behaviour you'd see if that variable wasn't set. Commented Aug 4, 2017 at 8:06

2 Answers 2


Now, based on your full code, there's 3 problems:

Line 60 of your code is missing a semi colon and throwing a fatal error. So:

$instance['description_check_box'] = $new_instance['description_check_box'] 

Should be:

$instance['description_check_box'] = $new_instance['description_check_box']; 

On line 66, you're using strings instead of proper boolean true or false values. Both if these are 'truthy' (see this answer to another question for what that means), so will both resolve as true if used in an if statement. if ('false') is true. So:

$description_check_box = $instance[ 'description_check_box' ] ? 'true' : 'false';

Should be:

$description_check_box = $instance[ 'description_check_box' ] ? true : false;

On line 121 you haven't got on in quotes, so you're comparing $description_check_box to the constant on. This should be in quotes if you want to check if the value of $description_check_box is on. So:

<?php if($description_check_box==on){ ?>

Should be:

<?php if($description_check_box=='on'){ ?>

BUT. This still won't work. Because you'd be setting $description_check_box to true or false based on whether it's checked on line 66, but later you're checking if the value is 'on'.

You either need to set the value of $description_check_box to 'on', or change your comparison to checking if $description_check_box is true.

You should also use isset() before using $instance['description_check_box'], because otherwise you'll get an undefined index notice in the customiser when WP_Debug is enabled.

Refer to my other answer for my approach, which avoids all these problems.


Here's a full implementation of a simple widget with a checkbox that hides/shows something on the front end:

class Checkbox_Widget extends WP_Widget {
    public function __construct() {
            'Checkbox widget',
                'classname' => 'widget_checkbox',

    public function widget($args, $instance) {
        $checkbox = isset( $instance['checkbox'] ) ? $instance['checkbox'] : false;


        if ( $checkbox === 'on' ) {
            echo 'I\'m checked!';


    public function form( $instance ) {
        $checkbox = isset( $instance['checkbox'] ) ? $instance['checkbox'] : false;

        <p><input type="checkbox" id="<?php echo $this->get_field_id( 'checkbox' ); ?>" name="<?php echo $this->get_field_name( 'checkbox' ); ?>" <?php checked( $checkbox, 'on' ); ?>>


    public function update( $new_instance, $old_instance ) {
        $new_instance['checkbox'] = $new_instance['checkbox'] === 'on' ? 'on' : false;

        return $new_instance;

Based on you answers to my comment, I think we're you're going wrong is not getting the current value of the checkbox correctly. See, in my example, how the value of $instance['checkbox'] is being set to on if it's checked, and then in the widget I'm checking if it equals on when outputting.

  • Take another look now, after seeing this comment, I copied the incomplete version of the code into my answer and it was full of mistakes. Should be ok now. Commented Aug 4, 2017 at 8:41
  • why some one downvoted you?
    – WordCent
    Commented Aug 4, 2017 at 8:58
  • ¯_(ツ)_/¯ Could be because they saw the original answer that was horribly wrong, or because I didn't really answer your question and just posted example code. If you post your full code, then I might be able to actually see what your problem is and explain it. Commented Aug 4, 2017 at 9:04
  • Your solution is not working.
    – WordCent
    Commented Aug 4, 2017 at 9:05
  • Yes it does. That exact class works and creates a widget that works correctly. The issue exists within your code, but unless you post all the code for your widget, no one can properly help. Commented Aug 4, 2017 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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