0

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 FeaturedPage\Custom;

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?

0

What if the theme has other stuff added and/or styled their sidebar for all the other core widgets and only works with display:block for example, or they set it to flex column where you might expect flex row / vice versa. I would suggest you add a wrapper to your widget content, if your widget content relies on a wrapper to be set a certain way, so you prevent breaking other widget's displays or display of the sidebar from theme to theme. Afterall there's no guarantee there that they even set a wrapper around the sidebar call like is shown above. You also don't have control over other plugins that might try to do this same thing - so then you would end up with incompatibility complaints.

Alternatively if you think this is really the best way to handle it for your particular situation, you could try using dynamic_sidebar_before and dynamic_sidebar_after hooks to add your own wrapper around dynamic_sidebar calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.