Skip to content

Instantly share code, notes, and snippets.

@Maxdw
Created November 19, 2015 16:48
Show Gist options
  • Save Maxdw/495cb02e761f233ca7a0 to your computer and use it in GitHub Desktop.
Save Maxdw/495cb02e761f233ca7a0 to your computer and use it in GitHub Desktop.
WordPress - Uses dynamic_sidebar to split widgets into an array allowing more control over widget placement.
/**
* Splits the HTML of each individual widget
* up into chunks of strings accessible by array.
*
* @param string $sidebar_id
* @return array
*/
function get_widgets($sidebar_id) {
$delimiter = '-' . md5(uniqid()) . '-';
$delimiter_cb = function($settings) use($delimiter) {
echo $delimiter;
return $settings;
};
ob_start();
add_filter('widget_display_callback', $delimiter_cb, 10, 1);
dynamic_sidebar($sidebar_id);
remove_filter('widget_display_callback', $delimiter_cb, 10);
$html = ob_get_clean();
$widgets = explode($delimiter, $html);
$widgets = array_filter($widgets);
$widgets = array_values($widgets);
return $widgets;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment