Widget Area: Modify $param[‘xy’] from widget-$n

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.

Related posts

Leave a Reply

2 comments

  1. 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 );
    }
    
  2. 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' );