Add class to specific widget in WordPress (footer customization example)

Add class to specific widget in WordPress (footer customization example)

Nowadays, many WordPress themes use horizontal sidebar areas or footer areas, not only a classic WordPress right or left vertical sidebars. This is good because theme users get more flexibility when they add content into their websites. But, developers must keep in mind that they sometimes need to provide specific widget behavior or styling depending of the place of a widget in some specific sidebar area.

Most common case scenario is a horizontal footer sidebar area, so what we are going to show here is how we can add “last” class to the last footer widget. You can of course use this example to modify it by your needs.

Introducing “dynamic_sidebar_params” filter

Any time your theme template files call dynamic_sidebar() function, it will be used to display all widgets assigned to a specific sidebar. WordPress uses dynamic_sidebar_params filter which will be called before any widget is displayed so you can use it to dynamically modify any widget.

Imagine that you have registered a sidebar for your theme footer which fits three widgets in one row. In the following example, we are going to show you how can you add last class to every third WordPress footer widget so you can style this widgets to fit the theme design. Just place this simple snippet inside your functions.php.

Hope this helps, feel free to leave your comments below!

Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

Leave a Reply

Your email address will not be published. Required fields are marked *