Context: This is my first widget and I am following along with an online tutorial. I am expecting the front-end of the widget to echo some content upon checking a check-box in the back-end form, but rather I am not seeing any content displayed.

Reference Material:


Back-end looks like this after saving

Back-end looks like this after saving

Front-end looks like this after saving Front-end looks like this after saving

Question: Why isn't the act of checking the check-box in the back-end UI resulting in the rendering of the string 'Checkbox is checked' on the front-end, as I expect it to?

  • Can you post your widget function's code? – hornj Jan 15 '14 at 22:26
  • $checkbox variable is undefined in your widget() function. As you did - $text = $instance['text']; $textarea = $instance['textarea']; do the same for $checkbox = $instance['checkbox'] too. – Shazzad Jan 15 '14 at 22:32
  • I am having trouble formatting the code in large blocks.Right now this is the best I can do, I will edit it later when I can find out how to properly format it. – Daniel Dropik Jan 15 '14 at 22:42
  • @Shazzad your are correct. My failure to define the $checkbox variable was the cause of the problem. After defining it as suggested, the widget worked as I exepected. Thank you. – Daniel Dropik Jan 15 '14 at 23:36

on line 49 it seems you have hardcoded in the checkbox value it should actually look like this, it was always going to be true even if it did work as intended:

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

also after line 81 add this in:

$checkbox = $instance['checkbox'];

Line numbers are based on what's in pastebin

  • thanks. You were correct about Line 81, and after defining it, everything worked as expected. However, regarding your comment about line 49, I am not so sure I follow. After fixing the problem on line 81 the original statement on line 49 worked well. – Daniel Dropik Jan 15 '14 at 23:38

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.