When using the filter:
add_filter('widget_display_callback', 'function_name');
function function_name($instance) {
/* Some Code */
return $instance;
}
Is it possible to determine the sidebar name and/or ID the particular widget instance is located within function_name
?
The widget_display_callback
hook takes 3 arguments as we can see in /wp-includes/widgets.php
: (line 180, WP 3.4.2)
$instance = apply_filters('widget_display_callback', $instance, $this, $args);
So, to make a complete hook, the code is:
// Priority = 10 // Arguments = 3
add_filter('widget_display_callback', 'wpse_57546_widget_display_callback', 10, 3);
// Using "$this" as an argument produces errors,
// as it is a reference to the current object
// see http://stackoverflow.com/q/1523479/1287812
// hence it is renamed to "$thiz"
function wpse_57546_widget_display_callback( $instance, $thiz, $args )
{
/* Some Code */
return $instance;
}
Using FirePHP, we see that this filter runs in all widgets being displayed in the frontend, and the following are the values of the widget “Categories”:
$instance = array(
['title'] =>
['count'] => 0
['hierarchical'] => 0
['dropdown'] => 0
)
$thiz = WP_Widget_Categories(
id_base="categories"
name="Categories"
widget_options = array(
['classname'] => 'widget_categories'
['description'] => 'A list or dropdown of categories'
)
control_options = array(
['id_base'] => 'categories'
)
number = 2
id = 'categories-2'
updated =
option_name="widget_categories"
)
$args = array(
['name'] => 'Main Sidebar'
['id'] => 'sidebar-1'
['description'] =>
['class'] =>
['before_widget'] => '<aside id="categories-2" class="widget widget_categories">'
['after_widget'] => '</aside>'
['before_title'] => '<h3 class="widget-title">'
['after_title'] => '</h3>'
['widget_id'] => 'categories-2'
['widget_name'] => 'Categories'
)
The values you are looking for are inside $args
:
$args['id']
$args['widget_id']
And to access $thiz
values, you’d use:
$thiz->id_base
$thiz->widget_options['classname']