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

Christopher Ross cross at thisismyurl.com
Tue Sep 20 18:13:28 UTC 2011


Thanks Otto! As always, there's a function in WP to do just about everything :)


On 2011-09-20, at 2:32 PM, Otto wrote:

> 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
>> 
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers




--
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



More information about the wp-hackers mailing list