
Every now and again you read a post that makes such an obvious point that you wonder why it hasn’t been made before. The benefits of hooks over plugin-specific functions is one of them.
WordPress hooks, whether they be actions or filters, are simple bits of code that plugin authors can use to attach events to. Most of these hooks are deep within functions in the WordPress core, but some are within the standard template files. The following line comes from the default comment form:
[php]
do_action('comment_form', $post->ID);
This particular hook is used to add extras, such as the subscribe to comments tick box, at the bottom of the comment form.
The usual course of action if there isn’t a hook where a plugin author wants one is simply to define a function and ask the blog owner to insert it into their theme. This does cause some problems though, the biggest being that when you switch the plugin off the function will cease to exist and error messages will sully your theme. Not ideal.
The approach that W-Shadow is advocating is that instead of writing a new function, the plugin author should attach events to a hook that doesn’t exist yet and ask the blog owner to insert the hook instead. That way, when the plugin is deactivated, no errors will occur. But even better, if plugins that want to achieve similar things all use the same hook then they will be interchangeable.
My plugin doesn’t have the one killer feature that you want, but a similar plugin does? Just turn mine off and the other on. Job done.
I haven’t had much time to really think it through, but it seems to me that a really good extension of this would be for plugin developers to get toghether to define a load of hooks to help make the benefits a reality. For example, plugins that create menus could attach to hooks called ‘main_menu’, ’secondary_menu’, or ‘footer_menu’.
I do think there is scope for this to bring about some interesting results.