I have set WP debug to true, and I am getting error message →

Notice: Undefined index: tick_the_snippet_type

        <?php checked( $instance[ 'tick_the_snippet_type' ], 'tick_snippet_4' ); ?>
        id="<?php echo $this->get_field_id( 'tick_snippet_4' ); ?>"
        name="<?php echo $this->get_field_name('tick_the_snippet_type'); ?>"
    <label for="<?php echo $this->get_field_id( 'tick_snippet_4' ); ?>">The Widget #3</label>

This is the line that is throwing the error →

<?php checked( $instance[ 'tick_the_snippet_type' ], 

What is the Fix for this?

The code update as requested →

public function update($new_instance, $old_instance) {
    $instance = $old_instance;  
    $instance['tick_the_snippet_type'] = $new_instance['tick_the_snippet_type'];
    return $instance;
  • Show the rest of your code. Where does the $instance come from? – Krzysiek Dróżdż Jun 18 '18 at 10:12
  • Updated. Please let me know If additional input is needed. – The WP Intermediate Jun 18 '18 at 10:48
  • and the rest of it? Especially constructor – Krzysiek Dróżdż Jun 18 '18 at 11:06
  • I am driving way back to home and stuck in traffic. I will update it soon. – The WP Intermediate Jun 18 '18 at 11:08

Whenever you use a value from an array, like $array['key'] where it's possible that the 'key' doesn't exist (such as the first time a widget is used) you need to check that it exists before using it or you'll get errors on those occasions where it hasn't been set.

Your problem is that when the widget's first added 'tick_the_snippet_type' doesn't exist on $instance, so you need to account for this possibility. I'd suggest creating a variable to pass to checked() which will either contain tick_the_snippet_type or a default if it doesn't exist:

if ( isset( $instance['tick_the_snippet_type'] ) ) {
    $value = $instance['tick_the_snippet_type'];
} else {
    $value = false;

Which can be written on one line with a ternary operator:

$value = isset( $instance['tick_the_snippet_type'] ) ? $instance['tick_the_snippet_type'] : false;

And then use this for checked:

<?php checked( $value, 'tick_snippet_4' ); ?>

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.