When to check if a function exists

I would not do this to prevent naming conflicts: if another unrelated plugin uses the same name, it’s unlikely that they will provide the same functionality and your plugin can continue as normal. There you want to fail early, not sneaky later on. Or prevent the conflict with a better prefix of course 🙂

The only reason to do this is to create a pluggable function, a separate piece of functionality that can be implemented in a different way by an additional plugin. You can see this in the WordPress core code: functions like wp_mail() are pluggable so you can use a completely custom mailing system.

You also see this in themes that are likely to get child themes. The default themes, Twemty Eleven and Twenty Ten before it, are good examples of this. Child themes can define functions with the same name, and they will be used instead.

Personally, I don’t really like this approach to provide hooks for other code. They make it harder to figure out what code will be called (an my IDE code completion gets confused). I try to provide all extension points via “classic” actions and filters.

Leave a Comment