1

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">
    <ul>
        <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>
    </ul>
</div>

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

<?php
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?

}
2
  • Search for Widget Context Plugin, I know Semiologic has one and there is another one. Might have some code you're interested in.
    – hakre
    Commented Jul 5, 2011 at 0:53
  • Thanks hakre. I've got the latest version of sem-reloaded but no widget context there. The "widget context" plugin is my original inspiration but it does way more than I need and I'm having a real tough time deconstructing it. He doesn't even use filters (in_widget_form, widget_update_callback or widget_display_callback) which is throwing me.
    – N2Mystic
    Commented Jul 5, 2011 at 1:20

1 Answer 1

4

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;
   ...

   if($show_it)
     return $instance;

   else
     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...

6
  • Thanks! Especially for the display_callback example code - makes perfect sense. I'm really stuck on the update callback though.
    – N2Mystic
    Commented Jul 4, 2011 at 22:27
  • you don't need to check for $_POST. Just pass 3 arguments to your update function: instance, $new_instance, $old_instance. $new_instance acts like $_POST, and $instance are your current widget options, which you need to update with $new_instance values, then return them (WP will update the options in the db)... Commented Jul 4, 2011 at 22:40
  • OK, need to take it a bit slow. How/where to I define the instances? Inside wse_widget_context_form() or as arguments on wse_widget_context_callback($instance, $new_instance, $old_instance)?
    – N2Mystic
    Commented Jul 4, 2011 at 22:50
  • function wse_widget_context_callback($instance, $new_instance, $old_instance){ ... Commented Jul 4, 2011 at 23:12
  • I'll do some more research on passing the $instances to the callback. I've updated my example code, but I'm getting "missing argument" warnings on the 2nd and 3rd arguments.
    – N2Mystic
    Commented Jul 5, 2011 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.