[wp-hackers] Object Oriented Plugin Feedback

Ryan McCue lists at rotorised.com
Sun Sep 16 03:04:34 UTC 2012


For what it's worth, we discussed this sort of concept a while ago on
http://kovshenin.com/2012/hey-wordpress-how-about-a-wp_plugin-class/ and
https://gist.github.com/1626492, which culminated in
https://github.com/rmccue/Rotor_WPPlugin

scribu wrote:
> The problem with this:
> 
>     function action_admin_init__perform_setup_tasks(){
>         // perform some additional setup tasks on admin_init
>     }
> 
> 
> is: how do you set the hook priority and the number of parameters the
> method accepts?

The latter is best taken by looking at the function itself. I personally
think having PHPDoc tags is much better than including it in the name,
so the aforementioned Rotor_WPPlugin classes map:

	/**
	 * @wp-action admin_init 10
	 * @wp-filter init 5
	 */
	function abc($a, $b) { }

to:

	add_action('admin_init', 'abc', 10, 2);
	add_filter('init', 'abc', 5, 2);


The only caveat with PHPDoc is that eAccelerator has an insane
configuration mode which strips PHPDoc blocks, despite them being
completely different to comments, and part of the code. For that reason,
it also has the ability to use similar naming to this, but with a
customisable prefix; this mode maps "action_init" -> add_action('init',
...); with the "action_" part customisable.

I use that class in quite a few of my plugins since it's quick and
effective. Even in projects where I don't, I still use the @wp-action
and @wp-filter tags, since it saves having to look up how it's being
hooked every time, and it also makes it much easier to search.

-- 
Ryan McCue
<http://ryanmccue.info/>


More information about the wp-hackers mailing list