Add a class to a dynamic sidebar’s wrapper

I am making a plugin to display featured pages of a site, but it depends on a flex container and i cannot assure that a user’s sidebar is going to have a display of flex.

However, looking for solutions to my problem, i found a filter called dynamic_sidebar_params, which indeed allowed me to change the class of the desired sidebar, that is, the one(s) containing my plugin’s widget. The problem is that this does not add a CSS class to the wrapper of the dynamic sidebar.

With the wrapper, i mean the element surrounding the dynamic_sidebar() function in the sidebar file. For instance:

sidebar-featured-pages.php

<?php
if ( ! is_active_sidebar( ‘featured-pages’ ) )
{
return;
} ?>

<section class=”featured-pages”>
<?php dynamic_sidebar( ‘featured-pages’ ) ?>
</section>

What i would like to do is modify or add a new class in that section element, or in whatever the user’s sidebar wrapper happens to be.

This is what i tried:

<?php
/**
* @package FeaturedPage
*/

namespace FeaturedPageCustom;

class Extras
{
/**
* Register default hooks and actions for WordPress
* @return
*/
public function register()
{
add_filter( ‘dynamic_sidebar_params’, [ $this, ‘add_sidebar_class’ ] );
}

/**
* Add a new CSS class to the sidebar containing the Featured Page widget(s)
* @param array $parameters The sidebar parameters array
* @return array $parameters The sidebar parameters array
*/
public function add_sidebar_class( $parameters )
{
if ( preg_match( ‘/featured_page_widget-{0,}/’, $parameters[0][‘widget_id’] ) )
{
$parameters[0][‘class’] = ‘featured-pages-flex-container’;
}

return $parameters;
}
}

This does add a class to the sidebar. However, it is not a CSS class, it is the class added when registering a sidebar. But in WordPress documentation it says about the class parameter: “CSS class applied to the sidebar container.”.

How can i get the sidebar’s wrapper? In this case, that section element with a class of featured-pages?

Read more here:: Add a class to a dynamic sidebar’s wrapper

Leave a Reply

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