[wp-hackers] Plugins: Best practice for conditional loading of actions and file includes

Ade Walker photofantaisie at gmail.com
Fri Feb 5 20:15:47 UTC 2010


Hi all,

This is not so much a support-type question, rather a question on best
practice concerning a specific aspect of plugin coding.

I use conditional is_admin() checks to determine whether the various PHP
files that make up my plugin should be loaded or not. For example, so that
"admin" files, eg settings pages etc, are only included by the main plugin
file if is_admin() is true. Of course, this works fine and as expected.

On the other hand, all of the hooks used by the plugin are declared in the
main plugin file - but are not wrapped in any is_admin() conditional checks.
This results in a situation where, when is_admin() is false, an "admin"
related hook, eg admin_menu, references a callback function which is defined
in a file which is not included (thanks to the conditional check mentioned
above). Normally, this doesn't throw any WP or PHP errors.

However, I've noticed that errors can be produced if another plugin makes a
direct call to wp-load.php. For example, Kimili FlashEmbed and other plugins
which launch a Add Media-type popup screen. In this case, my plugin
generates call_user_func_array errors, referencing my "orphaned" callback
functions, in the other plugins' popup screens.

Wrapping my hooks in is_admin() checks cures this problem, but it got me
thinking - why are the errors thrown only by the other plugins? Was I wrong
not to use conditionals to load the hooks? (With hindsight, probably, yes.)
Or is there something wrong in the way the other plugins are using
wp-load.php instead of some other method? Or both? :-)

Thanks for any thoughts on this.

Ade.


More information about the wp-hackers mailing list