Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have different widget areas. One is a horizontal area that may contain eg. 4 widgets next to each other. My Question is how would i add a different eg. $param['widget_title'] to widget number $n (via a filter)?

I already tried using wp_registered_widgets(); function and te different globals, but with no result.

share|improve this question
1  
The dynamic_sidebar_params filter is called before each widget is displayed, I think you can use that to achieve what you want. It filters the $args and $instance parameters that will be passed to the widget. –  Jan Fabry Apr 18 '11 at 15:21
    
@Jan Fabry - i already tried using the dynamic_sidebar_params filter with no result. Could you share an example? Thanks! –  kaiser Apr 18 '11 at 15:42
    
@kaiser: Only if you show me your failed attempt first :-) A little code snippet would also explain what you are trying to do better. –  Jan Fabry Apr 18 '11 at 15:43
    
@Jan Fabry - see updated Q. :) –  kaiser Apr 18 '11 at 16:09
    
@Jan Fabry - see another Update :) –  kaiser Apr 18 '11 at 16:29
show 4 more comments

2 Answers

up vote 1 down vote accepted

The dynamic_sidebar_params filter is called before each widget is displayed, so multiple times in the same sidebar. It filters the $args and $instance parameters that will be passed to the widget.

If you only want to execute it for the third widget of a specific sidebar, you should check for the sidebar ID (found in the id value of the $args array, which is passed first), and then count the widgets until you arrive at n. There are different ways to do this: you can get all widgets of the current sidebar and compare the ID of the n-th with the one you are filtering now, or you can just count it in your filter, resetting the count when you get a new sidebar name. Or you could replace dynamic_sidebar() in your template with a call to a function of your own that will first reset the widget count and then start filtering.

This is an example of the second type: counting widgets in the function, resetting the count each time we start a new sidebar:

add_filter( 'dynamic_sidebar_params', 'wpse15024_dynamic_sidebar_params' );
function wpse15024_dynamic_sidebar_params( $params )
{
    // These are the targets. Modify them here:
    $targeted_sidebar = 'sidebar-bottom';
    $targeted_widget = (int) 3;

    static $widget_counter = array();
    static $current_sidebar = null;

    $args = $params[0];
    $instance = $params[1];

    if ( $current_sidebar != $args['id'] ) {
        $current_sidebar = $args['id'];
        $widget_counter[$current_sidebar] = 0;
    }

    if ( $current_sidebar == $targeted_sidebar && $widget_counter[$current_sidebar] == $targeted_widget ) {
        $args['before_widget'] = '<div id="'.@$args['widget_id'].'" class="span-6 last">';
    }

    $widget_counter[$current_sidebar]++;

    return array( $args, $instance );
}
share|improve this answer
    
@Jan Fabry - i edited your answer slightly. I hope you don't mind (if so: sorry). –  kaiser Apr 20 '11 at 12:15
    
@kaiser: Ah, the first time I saw a peer edit and could vote on it! Accepted! –  Jan Fabry Apr 20 '11 at 12:53
    
@Jan Fabry - :) –  kaiser Apr 20 '11 at 13:17
    
@Jan Fabry - another edit (but i'll stop now) :) –  kaiser Apr 20 '11 at 13:26
1  
@kaiser: If you make the before_widget part configurable you would also need to make the right hand side (the <div>) configurable. If we continue on that road we might hide the part of the function that really matters (to me): counting the sidebar widgets and doing something when we reach our widget. I think this example is clear enough now for anyone who wants to adopt it in their code. –  Jan Fabry Apr 20 '11 at 13:51
show 1 more comment

This way everything works... but take a look at Jans approach.

That's the working code:

function wpse15024_modify_widget_params( $params )
{
    // These are the targets. Modify them here:
    $targeted_sidebar = 'sidebar-bottom';
    $targeted_widget = (int) 3;

    $sidebars_widgets = wp_get_sidebars_widgets();

    if ( $params[0]['id'] == $targeted_sidebar && $params[0]['widget_id'] == $sidebars_widgets[$targeted_sidebar][$targeted_widget] )
    {
        $params[0]['before_widget'] = '<div id="'.@$params[0]['widget_id'].'" class="span-6 last">';
    }

    return $params;
}
add_filter( 'dynamic_sidebar_params', 'wpse15024_modify_widget_params' );
share|improve this answer
add comment

Your Answer

 
discard

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.