Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created August 26, 2015 14:31
Show Gist options
  • Save bdeleasa/f6fa966ff73bd57e6b27 to your computer and use it in GitHub Desktop.
Save bdeleasa/f6fa966ff73bd57e6b27 to your computer and use it in GitHub Desktop.
Function for Wordpress that returns the number of active widgets in a sidebar.
<?php
/**
* Counts the number of widgets in a widget area.
*
* @param $sidebar_index string The id of the widget area
* @return int
*/
function get_widgets_count( $sidebar_index ) {
global $wp_registered_sidebars;
$index = "{$sidebar_index}";
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
return 0;
return count( (array) $sidebars_widgets[$index] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment