This question may seem trivial however as I see all of those styles very often I wonder if there is a difference (e.g. in performance or functionality):

Possibility 1:

function myabc() { }

Possibility 2:

function myabc() { }

Possibility 3:

add_action('init',function(){ });

Can you give a short explanation if any style may have an advantage about the other? Or are they overall equal?

  • It's harder to remove the action's callback with possibility 3, but one could also use $callback = function() {} and then remove_action( 'init', $callback );. – birgire Feb 21 '18 at 14:36

There is no difference between 1 and 2. Both are same in every single case. So, I'll consider 1 and 2 as A and 3 as B.

Now in A the add_action() callback function is a named function whereas in B the callback function is an anonymous function and this is the main difference. I hope you know that using remove_action() we can deregister an action hook and to do that we have to pass two parameters. One parameter is the hook name and another is the callback function name. So if you register a hook with an anonymous callback function it's not possible to deregister that later!

Hope it's clear now.

  • The missing possibility to deregister is a good point! It is also good to know that 1 and 2 are an entirely cosmetic matter of taste. – Blackbam Feb 21 '18 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.