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>
<?php
}
}
}
}
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
Thanks!
UPDATE
here’s the working function:
link to pastebin
3 s
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