Support » Plugins » Change Widgets based on Page or Category or Author

  • I would like to customize the widgets based on page, author, and category. So under the presentation–>widgets menu I would like to have a drop-down that allows me to choose a page, category, or author and add custom widgets for each so each page displays a unique set of widgets based on the page, author, or category.

    Anything like this available?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Never heard about anything like that.
    Of course, before the widgets were invented – everybody did that kind of things using the Conditional_Tags. However, that works only with the widgets off.

    I guess, you must decide what is more important: the convenience of the widgets or the customization.

    I think I know how to do this.
    in your template folder you should have a functions.php file. it has the widget call:

    if ( function_exists('register_sidebar') )
    		'name' => 'Right Sidebar',
    		'before_widget' => '
    		'after_widget' => '</ul>
    		'before_title' => '<li class="listHeader"><h2>',
    		'after_title' => '</h2>

    you can copy and past this again in the same file, change the name = Right Sidebar to home Sidebar for example.
    then in your sidebar.php put the home Sidebar in a conditional if home … this might give you an idea of where to start. I’ve just added a 3rd sidebar which actually is in side my header. you populate these bars with widgets in the admin/presentation/widgets page.

    I have been trying to hack together conditional widgets that don’t depend on the widgets themselves having code in them – so you can take a pre-canned widget and only have it appear on eg is_single() pages. I like the approach of having multiple sidebars defined, but it gets messy with lots of possible sidebars

    Anyway, I have something sort of working. This ‘proof of concept’ has 2 disadvantages as it stands:
    1) it is added to the widget control – so control-less widgets remain universal
    2) to make it flexible in this first version it uses PHP eval() which can run arbitrary code, so there might be security concerns.

    on the other hand it’s a simple hack as it stands, being only a few isolated lines in 2 files:

    Near the end of the file is a line <div id=”controls”> followed by “foreach ($wp_registered_widget_controls…”. Before the foreach insert:

    if(!$wl_options = get_option('widget_logic')) $wl_options = array();

    a few lines later find a line “call_user_func_array(…”. Right after that insert:

    if (isset($_POST[$widget['id'].'-widget_logic'])){
    update_option('widget_logic', $wl_options);
    echo '<p>Logic:<input type="text" name="'.$widget['id'].'-widget_logic" value="'.$wl_options[$widget['id']].'" /></p>';

    search for the line containing “is_callable($callback)”

    Change if ( is_callable($callback) ) { to if ($wl_value ) { and insert these line just before that:

    $wl_options = get_option('widget_logic');
    $wl_value=($wl_options[$id])?$wl_options[$id]:"return true;";
    $wl_value=(stristr($wl_value, "return"))?$wl_value:"return ".$wl_value.";";
    $wl_value=(eval($wl_value) && is_callable($callback));

    what it does
    It adds a “Logic” text field to the widget control in the admin interface. The text can be as simple as ‘is_single()’ or ‘is_single() || is_category(5)’ (I think that should work!). Basically the text is ‘eval’d logically before the widget is output – it should either be a full line of PHP that returns a value like ‘return true;’ or if it doesn’t contain the text ‘return’ then it adds ‘return’ on the front and a ‘;’ on the end

    Feedback on anyone trying this out most welcome!

    i’ve managed to make this into a plugin! i’ll play with it a bit more and then put it up somewhere. i haven’t browser tested it – as it relies on JS DOM manipulation to get the control in place.

    Alan, I would be very interested in this plugin!


    thanks. I’m downloading it now. will let you know.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change Widgets based on Page or Category or Author’ is closed to new replies.