WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to check if a registered sidebar has widget content - but can't seem to find a WP function for this - is_active_widget checks if it has content, but not if it exists or not.

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

    if ( $widget ) { // widget name passed ##

        // check if widget is active - return html & widget content if true ##
        if ( is_active_sidebar( $widget ) ) { 

        echo '
        <div class="'.$element.'">';

            dynamic_sidebar( $widget );

         echo '</div>';

        } else {

            // issue error if requested widget does not exist or is empty ##
            if ( ql_user_is_administator() ) { // user is admin ##

                if ( dynamic_sidebar( $widget ) ) { // empty ##

                    $message = 'empty';

                } else { // widget ID or name wrong ##

                    $message = 'error';


                <div class="error"><strong>Widget <?php echo $message; ?>:</strong> 
                <?php echo $widget_name ? $widget_name : $widget ; ?></div>


I call this on the page using:

// load widget ##
// widget_id, widget name, class of containing element ##
ql_widget( 'sidebar-general', 'Standard Sidebar', 'sider' );

The sidebars are registered correctly with register_sidebar() and have an unique ID assigned to each.

currently this function returns the error to the admin if the widget is missing or empty - I'd like to be able to tailor the error to indicate if it's missing or empty


UPDATE here's the working function: link to pastebin

share|improve this question
up vote 5 down vote accepted

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

share|improve this answer
bingo! - I'll update the answer to show the solution I've used - thanks – Q Studio Oct 17 '12 at 11:50
Isn't that what is_dynamic_sidebar is for? – userabuser Oct 17 '12 at 12:04
this allowed me to check all 3 states - wrong name, empty, display it if all is good... – Q Studio Oct 19 '12 at 10:51

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.


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;


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

share|improve this answer
thanks - I already am using that function - I want to display the sidebar if it has widgets, return an error if the sidebar is named wrong and do nothing if it's named correctly but empty. – Q Studio Oct 17 '12 at 11:45
thanks for the update - looks good :) – Q Studio Oct 19 '12 at 10:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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