Throughout the WordPress documentation I see callbacks being inserted as strings instead of function variables or closures. Let me examplify that;

function wpdocs_my_save_posts( $post_ID, $post, $update ) {
   // do stuff here
add_action( 'save_post', 'wpdocs_my_save_post', 10, 3 );

there's a bit of a catch here. This won't work and no editor (vscode) would be able to find out why. Whereas this would red-line:

$wpdocs_my_save_posts = function( $post_ID, $post, $update ) {
   // do stuff here
add_action( 'save_post', $wpdocs_my_save_post, 10, 3 );

as $wpdocs_my_save_post is not defined. enter image description here

as apposed to

enter image description here

What is the reason to use a string instead of a function variable in this case?

Not opinionated I just try to understand.

1 Answer 1


in general:

basically the answer is that php is dynamic language which do not force static constructs and do not validate them in any way until execution time. You will need extra tools to validate php code which a "bare" editor is unlikely to have.

And specifically to your question because that is how you reference callbacks in php since times lost. The fact that you might use some newer construct do not invalidate in any way the previous ways and do not make them inferior in all cases.

Wordpress specific:

One of the requirements for a good action callback is the ability to be able to "unhook" it. Unhooking is usually tricky in several kind of ways, but the first requirement is to be able to specify what you want to unhook. A clouser is almost an immidiate non starter although I am sure you can write some clever code to get its object id or other system itentifier and use it.

Which do not imply that clouser are bad, feel free (and probably better) to use it in code which is unlikely to require removal, for example when you write a site specific theme.

  • yes thanks. I came to understand the unhooking in WordPress is suboptimal. But it works fine with closures or function variables. This would allow early (development time) variable check. The reason I asked is we are in the process of generllising our theme development
    – th00ht
    Jan 23 at 19:46
  • 1
    you can probably check, just need better tools. Not sure if there is a tool that will discover the error in your specific example, but you can create a wrapper around adding and removing hooks that will be easier to statically check (maybe a base class for hooks). This will fly above most WP developers knowledge and experiance so again context of who develops and for what audiance is very important. Jan 23 at 19:54
  • Indeed I was thinking of creating a theme class of some sorts. To setup some kind of standardized CFT, menu-locations, logon-designs etc. etc.. A fn or closure based function could be easily be unhooked. I was wondering if there is a hidden reason why the Codex-docs don't mention other means to define callbacks.
    – th00ht
    Jan 23 at 20:36
  • 1
    hooking follows fully the php callback semantics. everything that can be used as a php callback can be used in a hook. being able to remove is the additional layer that makes it more complex Jan 23 at 20:41
  • 1
    it takes a while to get used to dynamically interpreted langs as they have more possibilities for dynamic execution which will be hard to implement in more static language. My background is C so I am fully aware of where you coming from, but things like $$foo(); which will excute a function which its name is stored in the variable foo are just not possible in C, but I think all "interpreted" langs can handle them. Jan 29 at 17:27

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.