[wp-hackers] Need WP/PHP guru eyeballs/feedback on my WP_Widget wrapper class

Nikola Nikolov nikolov.tmw at gmail.com
Sun Jun 30 06:33:39 UTC 2013


By the way you can also do the registration of all widgets in one function.
Store all of your widget classes in a static variable in your "Foo_Widget"
class - like "self::$registered_widgets[] = get_class( $this );"

And then you can add the action like this:

if ( ! has_action( 'widgets_init', array( 'Foo_Widget', 'register_widgets'
) ) ) {
    add_action( 'widgets_init', array( 'Foo_Widget', 'register_widgets' ) );
}

And then in your static "register_widgets" function, you can loop through
"self::$registered_widgets" and call register_widget() with each class
name.

Of course, you'll have to define "$registered_widgets" as a static array in
your class first :)


On Sun, Jun 30, 2013 at 1:15 AM, Micky Hulse <mickyhulse.lists at gmail.com>wrote:

> On Sat, Jun 29, 2013 at 2:47 PM, J.D. Grimes <jdg at codesymphony.co> wrote:
> >> Maybe if I passed __CLASS__ from the child class into the parent
> >> class's register_widget()? I'll have to play around with that.
> > I think you could do this instead of using __CLASS__:
> > $class = get_class( $this );
> > See here:
> > http://stackoverflow.com/a/1166592/1924128
> > I'm not sure if that works in all versions of PHP, but according to the
> poster there it works in 5.2.9.
>
> Ooooh, that's cool!
>
> get_class($this) looks really convenient. I'll play around with that.
>
> Thanks again J.D.! You rock! :)
>
> Have an awesome weekend.
>
> Cheers,
> Micky
> _______________________________________________
> 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