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.

I'm trying to include a checkbox in my widget back end. But I cannot get the value (on or off) after the user submits it. I thought the value would be stored in "esc_attr( $check )" (as it is when using a text input), but I cannot retrieve it.

This is what I'm trying now:

public function form( $instance ) {
    $check = isset( $instance[ 'check' ] ) ? $instance[ 'check' ] : 'off';
    echo esc_attr( $check ); // If the input is type text it outputs the value
    ?>
    <input class="widefat" id="<?php echo $this->get_field_id( 'check' ); ?>" name="<?php echo $this->get_field_name( 'check' ); ?>" type="checkbox" />
    <?php 
}

How can I get this to work? Also how do I get the value of the checkbox in the front end?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

First, on function widget:

function widget( $args, $instance ) {
    extract( $args );
    //ADD THIS
    $your_checkbox_var = $instance['your_checkbox_var'] ? 'true' : 'false';
    // etc
}

On function update:

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    //ADD THIS
    $instance['your_checkbox_var'] = $new_instance['your_checkbox_var'];
    //...
    return $instance;
}

Finally, on function form, add this:

<p>
    <input class="checkbox" type="checkbox" <?php checked($instance['your_checkbox_var'], 'on'); ?> id="<?php echo $this->get_field_id('your_checkbox_var'); ?>" name="<?php echo $this->get_field_name('your_checkbox_var'); ?>" /> 
    <label for="<?php echo $this->get_field_id('your_checkbox_var'); ?>">Label of your checkbox variable</label>
</p>

EDIT: Let's see that with one example (full code of a simple widget with one checkbox):

class about_us extends WP_Widget {

function about_us() {
    $widget_ops = array( 'classname' => 'about_us', 'description' => __('About Us', 'wptheme'));
    $this->WP_Widget( 'about_us', __('About Us', 'wptheme'), $widget_ops, $control_ops);
}

function widget( $args, $instance ) {
    extract( $args );
    $title = apply_filters('widget_title', $instance['title'] );
    $text = $instance['text'];
//This is the variable of the checkbox
    $avatar = $instance['avatar'] ? 'true' : 'false';

    echo $before_widget;

        if ( $title ) {
            echo $before_title . $title . $after_title;
        }
                    //Retrieve the checkbox
                    if('on' == $instance['avatar'] ) {
            ?><div class="about-us-avatar"><?php echo get_avatar( get_the_author_meta('user_email'), '50', '' ); ?></div><?php } ?>

                    <div class="textwidget">
                        <p><?php echo esc_attr($text); ?></p>
                    </div>

        <?php 
    echo $after_widget;
}

function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['text'] = strip_tags($new_instance['text']);
//The update for the variable of the checkbox
            $instance['avatar'] = $new_instance['avatar'];
    return $instance;
}

function form( $instance ) {
    $defaults = array( 'title' => __('About Us', 'wptheme'), 'avatar' => 'off' );
    $instance = wp_parse_args( (array) $instance, $defaults ); 
    ?>
    <p>
                <label for="<?php echo $this->get_field_id('title'); ?>">Title</label>
                <input class="widefat"  id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
    </p>
<!-- The input (checkbox) -->
            <p>
                <input class="checkbox" type="checkbox" <?php checked($instance['avatar'], 'on'); ?> id="<?php echo $this->get_field_id('avatar'); ?>" name="<?php echo $this->get_field_name('avatar'); ?>" /> 
                <label for="<?php echo $this->get_field_id('avatar'); ?>">Show avatar</label>
    </p>
    <p>
                <label for="<?php echo $this->get_field_id('text'); ?>">About Us</label>
                <textarea class="widefat" id="<?php echo $this->get_field_id('text'); ?>" rows="10" cols="10" name="<?php echo $this->get_field_name('text'); ?>"><?php echo esc_attr($instance['text']); ?></textarea>
            </p>
<?php
}
}
register_widget('about_us');

Tested and working.

share|improve this answer
    
Yes, I use it on a widget to enable/disable the avatar picture. It works very good for me. –  Gerard Sep 2 '13 at 16:25
    
Ok. I think for more clearness you should add in the answer the default assignation for $instance['your_checkbox_var'] on function form. –  G. M. Sep 2 '13 at 16:55
    
The default assignation is 'avatar' instead of 'your_checkbox_var'. I wrote 'your_checkbox_var', actually, to do it more clearness. Anyway I'm gonna modify my answer with the default example. Thank you for the advice :) –  Gerard Sep 2 '13 at 17:20

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.