[wp-hackers] Writing plugins with classes

Jamie Talbot wphackers at jamietalbot.com
Sat Feb 24 22:58:14 GMT 2007

Hash: SHA1

Aaron Brazell wrote:
> > Hey guys-
> >
> > Maybe someone can give me a quick pointer here. I've always wrote
> > plugins procedurally, but decided with this new one to write as a class.
> > Problem is, I'm obviously screwing up some syntax somewhere because the
> > class is being redeclared. There's really only two places it could be
> > happening and it boils down to WP syntax, not PHP... Help.

> > Fatal error: Cannot redeclare class myclass in
> > /home/emmense/public_html/wp-content/plugins/wp-103bees.php on line 11
> >
> > The only two places I could be redclaring is in the
> > add_management_page() call or the add_action() call...
> >
> > Help?

(Sorry if this a duplicate, I sent it from the wrong address the first time...)

Hey Aaron,

I remember similar problems when I began writing plugins.  There was a way round it, which I used in
X-Valid [1], but that was last edited a year ago, so things might possibly have changed since then.
 The solution I use now is to have all the guts of the plugin in a class, but use a separate file
for each admin page, like for Gengo [2].  This avoids your problem altogether, means the admin page
code isn't being loaded for every site view and means you didn't just have a single massive
monolithic file to edit, which can be a nightmare when you have more than one simple page.

[1] http://dev.wp-plugins.org/browser/x-valid/trunk/xvalid/xvalid.php - XHTML tag balancing and
cleaning for posts and comments (1 admin page, 1 feedback page).

[2] http://dev.wp-plugins.org/browser/gengo/trunk/gengo - Multilingual framework for WordPress (1
top level menu, 4 submenus).

Hope they help point you in the right direction,



- --
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the wp-hackers mailing list