Context aware widgets. My work in progress

I’m working on a simple addition to the widget options to allow the user to set the “context” in which widgets show be shown.

Here’s what I’ve got so far, with my questions in the comments…

1) Append the widget form to all widgets…

add_filter('in_widget_form', 'wse_widget_context_form');

function wse_widget_context_form($widget){
//echo 'the widget id is: '.$widget->id;
//do we need to reference the widget->id to avoid variable overwrites on multiple widgets?
<div class="wse_context">
        <li>Hide this widget on: </li>
        <li id='home'><label><input value="on" type="checkbox" name="noHome" id="noHome"<?php if(isset($noHome)) echo $noHome ?> /> home</label></li> 
        <li id='posts'><label><input value="on" type="checkbox" name="noPosts" id="noPosts"<?php if(isset($noPosts)) echo $noPosts ?> /> posts</label></li>
        <li id='pages'><label><input value="on" type="checkbox" name="noPages" id="noPages"<?php if(isset($noPages)) echo $noPages ?> /> pages</label></li>
        <li id='cats'><label><input value="on" type="checkbox" name="noCats" id="noCats"<?php if(isset($noCats)) echo $noCats ?> /> categories</label></li>

2) Create a callback function to handle when widgets are saved…(so far, this is the part I’m stuck on)

add_filter('widget_update_callback', 'wse_widget_context_callback');

add_filter('widget_update_callback', 'wse_widget_context_callback');
function wse_widget_context_callback($instance, $new_instance, $old_instance){
    echo 'instance: '.$instance.'<br/>'; // returns array
    echo 'new_instance: '.$new_instance.'<br/>'; //returns ''
    echo 'old_instance: '.$old_instance.'<br/>'; //returns ''

3) Show/Hide widget based on widget settings

add_filter('widget_display_callback', 'wse_widget_display_callback');

function wse_widget_display_callback()
//how do I read the values and unset widgets as appopriate?


Solutions Collecting From Web of "Context aware widgets. My work in progress"

Just like you would from a widget() method:

function wse_widget_display_callback($instance)

   $show_it = true;
   if(isset($instance['noHome']) && $instance['noHome'] && is_home()) $show_it = false;
   if(isset($instance['noPages']) && $instance['noPages'] && is_page()) $show_it = false;

     return $instance;

     return false;


I’ve posted here the functions I use to accomplish this, it might be helpful. The form hooks are here, but they are part of a class…