Skip to content

Instantly share code, notes, and snippets.

@rxnlabs
Last active May 30, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rxnlabs/4897ee1153534ca17b4860c142ff7886 to your computer and use it in GitHub Desktop.
Save rxnlabs/4897ee1153534ca17b4860c142ff7886 to your computer and use it in GitHub Desktop.
WordPress - Get the name and id of the widget area being used to display a certain widget. This adds the widget ID and widget area name as HTML comments to the frontend of the site
<?php
/**
* Add the widget ID and widget name as HTML comments.
*
* Makes it easiser to identify exactly which widget area a widget is appearing in.
* This helps a lot with themes that have a lot of sidebars and uses a lot of widgets.
*/
function which_dynamic_sidebar( $sidebar_params ) {
$sidebar_params['0']['class'] = empty( $sidebar_params['0']['class'] )?$sidebar_params['0']['id']:$sidebar_params['0']['class'].' '.$sidebar_params['0']['id'];
$sidebar_params['0']['before_widget'] = '<!--Widget-Area:id:'.esc_attr($sidebar_params['0']['id']).';name:'.esc_attr($sidebar_params['0']['name']).'-->'.$sidebar_params['0']['before_widget'];
return $sidebar_params;
}
add_filter( 'dynamic_sidebar_params', 'which_dynamic_sidebar', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment