Wordpress - check if registered sidebar is active & has widget content

You can check if a sidebar is active and in use (contains widgets with content) through using is_active_sidebar, here's an example...

if ( is_active_sidebar( $sidebar ) ) {
    // Display some text
} else {
    // Display none
};

Where $sidebar is either the name, ID or number of the sidebar you wish to check.

Update:

Here's a suggestion for improving your function's legibility,

function ql_widget( $widget, $widget_name, $element = 'sider' ){

    if ( $widget ) { 

        $message = (ql_user_is_administator() ? 'empty' : 'error');
        $widget_name = ($widget_name ? $widget_name : $widget);

        if ( is_active_sidebar( $widget ) ) { 

            $html  = '<div class="'.$element.'">';
            $html .= dynamic_sidebar( $widget );
            $html .= '</div>';
            echo $html;

        } else {

            $html  = '<div class="error"><strong>Widget ';
            $html .= $message;
            $html .= '</strong> ';
            $html .= $widget_name;
            $html .= '</div>';
            echo $html;

        }
    }
} // This is the end of the function

And if you only want the error message to appear if admin, then wrap the inner contents of the else statement with another conditional or change the else to elseif and run the check for an administrator and optionally finish off with an else condition to meet all other outcomes, such as for regular users. Codex reference: http://codex.wordpress.org/Function_Reference/is_active_sidebar


wp_get_sidebars_widgets(); will return a list of all sidebars & the widgets in each of them. This can allow you to check for sidebars with no widgets

Tags:

Widgets