[wp-hackers] Identify the sidebar from within a widget?
Dagan Henderson
Dagan.Henderson at epyllion.com
Tue Sep 20 17:35:34 UTC 2011
Building on Otto's reply, here is an example of $args:
$args Array [9]
name (string:15) Primary Sidebar
id (string:9) sidebar-1
description (string:66) A widget area displayed on the right side of most pages/templates.
before_widget (string:61) <li id="recent-posts-3" class="widget widget_recent_entries">
after_widget (string:6) </li>\n
before_title (string:25) <h1 class="widget-title">
after_title (string:5) </h1>
widget_id (string:14) recent-posts-3
widget_name (string:12) Recent Posts
-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com [mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Otto
Sent: Tuesday, September 20, 2011 10:33 AM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] Identify the sidebar from within a widget?
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
More information about the wp-hackers
mailing list