[wp-hackers] how to do a single-instance widget with the new API

Raphaël Droz raphael.droz at gmail.com
Fri Mar 19 09:22:53 UTC 2010


Andrew Nacin wrote:
> Before getting into multiple widget instances, I'd start with:
> http://codex.wordpress.org/AJAX_in_Plugins
>
> <http://codex.wordpress.org/AJAX_in_Plugins>Because:
> define('DOING_AJAX', true);
> include('../../../wp-config.php');
> include(WP_CONTENT_DIR . '/../wp-admin/includes/admin.php');
>
> Is not the right way to do it.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>   
Thank you Andrew for the link, that was very useful to me
(this link may have its place on the http://codex.wordpress.org/Plugins 
page)

On Wed, Mar 17, 2010 at 12:55 PM, Otto <otto at ottodestruct.com> wrote:

> Firstly, yeah, that's the wrong way to do AJAX. Like he said.
>
> Secondly, single instance widgets are the tool of the devil. Don't do
> it. Make it work multiply.
>
> Third: If you're inheriting from WP_Widget, then you have a $number.
> That $number is something you'll find useful for looking in that
> array.
>
> -Otto
The $number helped a lot, I put it in a hidden field but it doesn't 
solved all my problem :
I wanted to bind another plugin (MailPress) to this widget.
Since I then can't use any form to pass the instance information I did 
the following to find
the "last" (which in my case I assume "uniq") instance of the widget :
In widget update() : update_option('my_widget_last_instance', 
$this->number);
So I use this option in MailPress to find the right options of the number

(I may also use this get_option('my_widget_last_instance') in the 
server-side callback of the widget but
that's probably a workaround of not being able to define my 
'widget_php_callback' of
add_action('wp_ajax_widget_callback', 'widget_php_callback');
inside the class itself to 'natively' benefit from $number)

Maybe in my case I should assume the last index of my 
get_option('my_widget') is always
the active and valid (used) one.
Anyway I'm now fine with update_option('my_widget_last_instance') which 
should simply let
me do most of what I wanted.
My last things are "hook into 'activated_plugins' to ensure the order" 
and use the 'nonce'.

Thank you for your advices.

Raph


More information about the wp-hackers mailing list