When developing themes and plugins it is sometimes neccesary to add some functionality to some hook using conditional statements.
Example:
function my_custom_function() {
if( is_home()) {
<---what should the function do--->
}
}
add_action( 'some_hook', 'my_custom_function' );
To my understanding, whenever any other condition exists (is_home
return false), the content of the function is not executed, however the function is executed, although it is "empty". This means that an empty function is passed to the hook. This is the way that all examples are shown in the codex using conditional tags.
I do understand that this is safe to do so, and it should not have any significant impact on load times (if there is any impact on load time at all).
I have been thinking, the same piece of code, as per example, can be written as follow
if( is_home()) {
function my_custom_function() {
<---what the function should do--->
}
add_action( 'some_hook', 'my_custom_funtion' );
}
This will completely skip everything if is_home
returns false.
I don't mind using any of these two methods. But what I want to know, because of the first example been extensively used, are there any coding standard that states that this is the correct method to use, or is this rather the prevered way according to wordpress developers, or just personal preference.