WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've added a few checkbox options to the default widget form. But I'm stuck on how to access these option states and pass them along during the widget save event (widget_update_callback) so that they can be saved by WP to the DB.

How do I pass my custom form values to WP to save as widget options for each widget?

In the first function, I'm Appending my checkbox options to all widgets control panels...

add_filter('in_widget_form', 'wse_widget_context_form');

function wse_widget_context_form(){

$checked = ' checked="checked"';
<div class="wse_context">
        <li>Don't show this widget on: </li>
        <li><label><input value="on" type="checkbox" name="noHome" id="noHome"<?php if(isset($instance['noHome'])) echo $checked ?> />Home Page</label></li> 
        <li><label><input value="on" type="checkbox" name="noPosts" id="noPosts"<?php if(isset($instance['noPosts'])) echo $checked ?> /> Posts</label></li>
        <li><label><input value="on" type="checkbox" name="noPages" id="noPages"<?php if(isset($instance['noPages'])) echo $checked ?> /> Pages</label></li>
        <li><label><input value="on" type="checkbox" name="noCats" id="noCats"<?php if(isset($instance['noCats'])) echo $checked ?> /> Categories</label></li>

Callback function is below. $instance doesn't contain the custom form values, only the default widget values (text and content). How do I append my custom options to $instance?

add_filter('widget_update_callback', 'wse_widget_context_callback');

function wse_widget_context_callback($instance){
    echo 'instance: '.$instance.'<br/>'; // returns array but no custom form values

share|improve this question
What widget are you dealing with? – eddiemoya Jul 10 '12 at 23:17

Not sure if you're still struggling with this, but for anyone that is, here's the solution:

Add the following arguments to wse_widget_context_form(): $widget, $return and $instance

Then use the following to get field IDs and field names: <?php echo $widget->get_field_id('field_name'); ?> and <?php echo $widget->get_field_name('field_name'); ?> respectively. e.g.

<li><label><input value="on" type="checkbox" name="<?php echo $widget->get_field_name('noHome'); ?>" id="<?php echo $widget->get_field_id('noHome'); ?>"<?php if(isset($instance['noHome'])) echo $checked ?> />Home Page</label></li>

Hope that helps!

share|improve this answer

I think you are dealing with same functionality with http://wordpress.org/extend/plugins/widget-context/

you can check it out for the structure of code on how it does

I hope this give idea and help

share|improve this answer

Your Answer


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.