WordPress.org

Ready to get started?Download WordPress

Forums

Display Widgets
[resolved] is_active_sidebar not working with this plugin. (7 posts)

  1. carlosdevia
    Member
    Posted 1 year ago #

    When i use this plugin to hide some widgets in a sidebar the function is_active_sidebar still says that the sidebar is active, even when it doesn't have any widget active using this plugin.

    http://wordpress.org/extend/plugins/display-widgets/

  2. mattscottdesign
    Member
    Posted 1 year ago #

    In case anyone else is struggling with this, I wrote this function as an alternative to is_active_sidebar() for the Display Widgets plugin. The comments explain it all. Stick this in your functions.php file, and then use has_visible_widgets() instead of is_active_sidebar().

    /* The Display Widgets plugin doesn't update
     * is_active_sidebar() for hidden widgets.
     * This function tests dynamic_sidebar() to
     * see if it is empty. Use instead of
     * is_active_sidebar() to determine if the
     * sidebar has any widgets. Remember to
     * change back to is_active_sidebar() if no
     * longer using Display Widgets.
     */
    function has_visible_widgets( $sidebar_id ) {
    
        // First check if sidebar has any widgets
        if ( is_active_sidebar($sidebar_id) ) {
            // Use PHP output buffer to load
            // the sidebar into a variable
            ob_start();
            dynamic_sidebar($sidebar_id);
            $sidebar = ob_get_contents();
            ob_end_clean();
            // Return false if sidebar is empty
            if ($sidebar == "") return false;
        } else {
            return false;
        }
    
        // Return true if sidebar is not empty
        return true;
    
    }
  3. SpankMarvin
    Member
    Posted 6 months ago #

    mattscottdesign, thank you thank you! I needed a way of adding a body class ONLY if a certain sidebar was explicitly present on each page. This allowed me to do it.

    Many thanks for following up and posting this solution.

  4. Giorgio25b
    Member
    Posted 6 months ago #

    Mattscottdesign, thanks so much.
    @ SpankMarvin: would you mind to share your solution?

    I'd love to do something similar to what you described.
    Thanks in advance
    G.

  5. SpankMarvin
    Member
    Posted 6 months ago #

    My code is here:

    http://pastebin.com/QHLSxvse

    Basically, it hooks into wp_head, runs the function above, and if it returns true, it adds a class to the body tag. This allows me to adjust the width of my content areas accordingly with CSS. There would be other more straightforward solutions but I had to work with this due to the way a developer had setup the sidebars to work.

    Hope this helps.

    J

  6. Giorgio25b
    Member
    Posted 6 months ago #

    Awesome! Thanks so much... this makes the day!!

  7. sswells
    Member
    Plugin Author

    Posted 6 months ago #

    is_active_sidebar() is now supported in v2.0. Enjoy!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic