[wp-hackers] Identify the sidebar from within a widget?

Otto otto at ottodestruct.com
Tue Sep 20 17:32:35 UTC 2011


If you're using a standard widget class inheriting from WP_Widget,
then the widget($args, $instance) function which displays the widget
receives $args as its first parameter.

$args contains info about what sidebar you're in. Specifically,
$args['name'] and $args['id'] refer to the sidebar's name and id.


Demo:

class New_Widget extends WP_Widget {
	function New_Widget() {
		$widget_ops = array('classname' => 'widget_new-widget',
'description' => 'New Widget');
		$this->WP_Widget('new-widget', 'New Widget', $widget_ops);
	}

	function widget($args, $instance) {
		var_dump($args);
	}

	function update($new_instance, $old_instance) {
		return $new_instance;
	}

	function form($instance) {
	}
}
add_action('widgets_init', create_function('', 'return
register_widget("New_Widget");'));


Look at what the widget dumps for output.

-Otto



On Tue, Sep 20, 2011 at 12:15 PM, Christopher Ross
<cross at thisismyurl.com> wrote:
> Is there a way for me to know which sidebar calls a specific widget?
>
> For example, I have a widget that needs specific attributes if called from the footer as opposed to the sidebar. So, instead of asking the user to set "sidebar" or "footer", is there a way to tell that the widget call came from one or the other?
>
> c.
>
>
> --
> Christopher Ross
> christopher.ross at rodonic.com
>
> Rodonic - http://rodonic.com
> My Blog - http://thisismyurl.com
>
> Twitter - http://twitter.com/#!/thisismyurl
> Facebook -  http://www.facebook.com/thisismyurlcom
>
> (416) 900-3731
> (858) 201-4912
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list