Steve Taylor photo

Always test for existence of plugin functions

Like every developer, I’ve got a particular configuration for most sites I deploy. My WordPress sites are mostly all based on my Pilau parent/child themes, and I’ve a set of “usual suspect” plugins I include in most projects. Some plugins—in particular Developer’s Custom Fields—are pretty much indispensable to my work. So much so, that for a while I assumed, in my custom theme code, that the plugin would be installed.

More recently I’ve forced myself to follow best practices better. So, even if I know a certain plugin will be installed and active, because I manage the site, I still surround a call to a function from that plugin with a function_exists() test. I just found out why that’s a good idea.

I just released the 0.9 version of Developer’s Custom Fields, and updating an older site, with some “untested” function calls, what happened was this. The plugin update happened fine. But when it came to reactivating the plugin, the update screen’s iframe, running a request without the plugin being active, hit some code that’s run on every request and calls a function from this plugin. So instead of showing “Plugin reactivated successfully”, there was a fatal PHP “function does not exist” error. And since this stopped the reactivation process in its tracks, the site was now completely broken.

Now, this happened on my local dev copy of the site, so I just added the function_exists() tests that should have been there anyway, and reactivated the plugin with some manual phpMyAdmin trickery. But in other circumstances, this could be disastrous.

So, even if it seems like a bind: always test for the existence of a plugin function.