Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created December 24, 2011 18:18
Show Gist options
  • Save GaryJones/1517997 to your computer and use it in GitHub Desktop.
Save GaryJones/1517997 to your computer and use it in GitHub Desktop.
Add content to the top of each sidebar on the Widgets admin screen.
<?php
add_action( 'dynamic_sidebar', 'gmj_add_content_to_top_of_sidebars_on_widgets_screen' );
/**
* Add content to the top of each sidebar on the Widgets admin screen.
*
* @author Gary Jones
*
* @global array $wp_registered_widgets All registered widgets
* @param array $current_widget The current widget about to have its form shown
* @return null Returns null if not admin, or not the first widget in the sidebar
*/
function gmj_add_content_to_top_of_sidebars_on_widgets_screen( array $current_widget ) {
global $wp_registered_widgets;
// Only run on the Widgets admin screen, not the front-end
if ( ! is_admin() )
return;
// Get all sidebars and their widgets
$sidebars_widgets = wp_get_sidebars_widgets();
// Optionally remove looping through Inactive Widgets
unset( $sidebars_widgets['wp_inactive_widgets'] );
// Get current sidebar ID
foreach( $sidebars_widgets as $sidebars => $widgets ){
for( $i = 0; $i < count( $widgets ); $i++ ) {
if ( $current_widget['id'] === $widgets[$i]) {
$current_sidebar_id = $sidebars;
break 2;
}
}
}
// Bail if sidebar not found (e.g. Inactive Widgets, which we unset earlier)
if ( ! isset( $current_sidebar_id ) )
return;
// Get first widget ID in the current sidebar
foreach( $sidebars_widgets[$current_sidebar_id] as $key => $value ) {
$first_widget_id = $value;
break;
}
// Bail if we're not about to show the first widget form
if ( $first_widget_id !== $current_widget['id'] )
return;
// Now echo something awesome at the top of each sidebar!
echo '<p class="sidebar-awesomeness">Foobar for ' . $current_sidebar_id . '!</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment