WordPress: Apply filter/hook to a particular sidebar widgets?

I’m developing a theme that has more than 5 different sidebars and I want to apply a function to a particular one of them for styling purposes. Basically, the function will modify it’s params using a counter to show random ‘s between each of the widgets. I came up with that:

function widget_params( $params ) { 
    // ...
}
add_filter( 'dynamic_sidebar_params', 'widget_params' );

But however, it will run for each and every different sidebar. Is there any way to make it work only on a particular sidebar? Or alternatively, is there any way to get current widget’s sidebar id inside of that function?

Read More

Hope all of that makes sense.

Thanks in advance!

Related posts

Leave a Reply

1 comment

  1. I’d recommend creating a custom hook and placing it in the appropriate sidebar template. See do_action for more information. Use the following line in the sidebar template:

    <?php do_action('custom_sidebar_action', 9999); //Replace X with the sidebar ID parameter you want to pass to the function. ?>
    

    Then, in your functions, modify your code to be something like the following (Just coding from memory, so might take some finessing):

    function my_widget_function( $sidebar_id ) { 
        // ...
    }
    add_action('custom_sidebar_action', 'my_widget_function', 10, 1);
    

    With this method, I’m guessing you could get away without passing the sidebar ID, so the cleaner function would look like this:

    <?php do_action('custom_sidebar_action'); ?>
    

    Then:

    function my_widget_function() { 
        // ...
    }
    add_action('custom_sidebar_action', 'my_widget_function');