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

Micky Hulse mickyhulse.lists at gmail.com
Tue Jun 25 17:55:42 UTC 2013


Hello,

Currently, I have a setup for custom widgets where I create a simple
class for each new widget. For example:

class.widget_1.php
class.widget_2.php
class.widget_3.php
...

While the above works well, I'm finding that I'm repeating a little
bit of boilerplate code for each widget (even after extending an
abstract class with most of the boilerplate in that).

In an attempt to improve my code, and avoid repeating myself, I
created this class:

<https://gist.github.com/mhulse/5860561>

Inspired by WPAlchemy, I thought it would be cool to create my custom
widgets like so:

[code]

$medium_rectangle_1 = new foo_Widget(
    array(
        'id_base' => 'medium_rectangle_1',
        'name' => 'Medium Rectangle 1',
        'description' => 'Standard Advertising Unit',
        'template' => STYLESHEETPATH .
'/includes/widgets/medium-rectangle-1.php',
    )
);

[/code]

Unfortunately, I keep getting this PHP notice:

[snip]

Notice: Undefined property: Foo_Widget::$option_name in
/.../wp-includes/widgets.php on line 291 Warning: array_merge():
Argument #2 is not an array in /.../wp-includes/widgets.php on line
777

[/snip]

... and the widget does not show in the admin (well, there's an empty,
useless, widget box with no label).

Now, I know my code could use some more work (I'm kinda pushing the
limits of my WP/PHP skillz) ... I just wanted to see if anyone here
had any tips as to what I might be doing wrong?

Thanks!
M


More information about the wp-hackers mailing list