I Know it's not needed if we're using the latest wordpress but I'm just wondering if it's a good practice to always use it or just use new 3.0+ functions without it, and should we use it for commercial themes ?
thanks in advance.
WP currently has ~2.5k functions. So if you'd check for every function existence on runtime, then you'd really slow things down.
When you're looking at wp core or some themes and it's »pluggables«, then you'll see that those are wrapped inside if ( function_exists('fn_nam') )
calls.
The reason to do this is to allow overwriting of functions in plugins, themes or child themes.
So if you want to let people alter stuff, then you'll want to wrap them up, so they don't get used when there's already a (child theme) function replacing it.
Don't do this for core functions. Core functions (or some of their arguments) have the call to…
_deprecated_argument($function, $version)
_deprecated_file($file, $version)
_deprecated_function($function, $version)
…for a reason: Save execution time, provide feedback for developers and a smooth running system for users as those won't be outputted if WP_DEBUG
isn't set to TRUE
.
So: Only do this for functions that you want people be able to replace.
function_exists()
throughout your theme.
Using function_exists()
is very important when you developing a theme that could be used a parent theme or a plugin, that you anticipate that other plugins will override the functionality of the plugin.
If you a developing a parent theme, wrapping each function in a
if (!function_exists())
conditional, allows child themes to override the parent themes functionality, by using the same function names. The child's functions.php
will be loaded first, so then when the parent's functions.php
gets loaded, the conditionals will fail, because the functions already exist.
There isn't a true object oriented hierarchy available in Word Press, but this gets close.