I recently started to develop plugins and themes and I found that I need to use several functions of on both.
Sometime I think about to check if function / class exist before declared as said on this post: When to check if a function exists
But that is considered as bad practice. What is best practice to prevent conflicts and keep themes & plugin work independently without one themes / plugin installed?
Actions & Filters
The imho best way is to use an action to bring plugin functions into themes.
Example #1
Here’s a little plugin to test this.
Inside the theme:
What now happens / The kool kid
This way we don’t have to check the existence of a function, a file, a class, a method or maybe even a (don’t do this!) global
$variable
. The WP intern global already takes this for us: It checks if the hook name is the current filter and attaches it. If it doesn’t exists, nothing happens.Example #2
With our next plugin, we’re attaching a callback function that takes one argument.
Inside the theme:
This time, we offer the user/developer the possibility to add an argument. He can either
echo/print
the output, or even process it further (in case you got an array in return).Example #3
With the third plugin, we’re attaching a callback function that takes two arguments.
Inside the theme:
This plugin now allows us to insert two arguments. We can save it into a
$variable
and further process it.Conclusion
With using filters and actions you’re giving better performance by avoiding unnecessary checks (compare speed of
function_*/class_*/method_*/file_exists
or the search for a global within_array()
for ~1k(?) filter searches). You also avoid having all those unnecessary Notices for not set variables, etc., as the plugin cares about this.