$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Add a class to a dynamic sidebar's wrapper|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Add a class to a dynamic sidebar's wrapper

matteradmin9PV0评论

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?

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?

Share Improve this question asked Nov 9, 2018 at 19:41 ManuAlvarado22ManuAlvarado22 1416 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Post a comment

comment list (0)

  1. No comments so far