[wp-hackers] Best Plugin Template - For Developers
Mike Schinkel
mikeschinkel at newclarity.net
Thu Dec 23 12:39:53 UTC 2010
I've been evolving my plugin style a lot since WordPress Answers launched as I have been answering lots of questions over there. I'm sure others on this list will prefer their own styles which will likely conflict with mine but I'll explain what I've come up with as I'm really satisfied with it in case it can help you:
Basically I use a class to encapsulate with a static function "on_load()" that I call immediately after the class definition. The on_load() function then add all hooks (all actions and all filters) and I use static functions for those hooks since there is no real need for instance functions (having an instance hanging taking up a name in the global namespace just to enable you to wire up hooks was something I wanted to avoid.)
I typically name the functions the same name as the hook but with a prefix of "action_" or "filter_" as appropriate, i.e. static function action_init() handles the 'init' action hook. (Following those conventions I've actually written code that will wire up all the hooks in the class with single call to a helper function in on_load(), but I haven't started using that in my production code yet nor have I show an example of it on WordPress Answers.)
So for the most part I'm using all static functions for the class that wraps the hooks for a plugin, but I will also use instances of classes if there is a need for them. Still using instances of class is an approaches you can use for any PHP code and thus is not specific to WordPress plugin best practices. Of course I don't think my plugin coding style will work with PHP4 so bear that in mind.
If you want to see the coding style this answer gives a good example of what I was just explaining (sans the auto-wireup of the hooks):
http://wordpress.stackexchange.com/questions/5750/#5778
Hope this helps.
-Mike
On Dec 23, 2010, at 6:14 AM, Alex Hempton-Smith wrote:
> I would find this very useful as well. Any plugins that devs could flag as
> 'best practice'?
>
> I did hear a while back that somebody was going to create a dummy plugin for
> this very reason?
>
> -- Alex
>
>
> On Mon, Dec 20, 2010 at 12:36 PM, matthew engel <
> matthew.thomas.engel at gmail.com> wrote:
>
>> Hello,
>>
>> I've been developing plugins for a while, but now looking to polish my
>> skills a bit. I would also like to build a plugin to release on the
>> WordPress site, however I was wondering which plugin out there has the
>> best structure (classes, files, documentation standards, etc...) from
>> a developers perspective.
>>
>> Thank you,
>>
>> Matt
>> _______________________________________________
>> 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