Make WordPress Core

Opened 16 months ago

Last modified 16 months ago

#40156 new enhancement

Warning for invalid hook callback is not very useful

Reported by: Shelob9 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 0.71
Component: Plugins Keywords:
Focuses: Cc:


If you add a hook with an uncallable callback, an error like Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'not_a_function' not found or invalid function name in is shown.

This is technically correct, but not useful. When I say useful, I don't think it is useful to the following user profiles:

1) Non-technical user seeing errors. A non-developer might no what a WordPress hook is, but not how it works internally. Therefore they are not likely to understand this error is related to a hook. They are not given good information, and might be missing a clue as to who to ask for support (plugin or theme developer, host, etc.)

2) New developer. A developer new to WordPress might not understand yet what a hook is or that a missing callback generates this type of error. A more clear error will help guide their googling for the error and/ or help them find the issue in the code. 3) Experienced WordPress developer. When I see this error, I assume there is a non-callabale hook callback. But call_user_func() has other uses so it might be misleading to me.

I think it would be way more helpful if the error triggered was something like Hook $hook_name could not call $callback or The callback function $hook, registered for $hook_name is not callabale

Change History (3)

#1 @johnbillion
16 months ago

This is a good suggestion, but I think the reason the hooks and filters API doesn't first check the callback function with is_callable() is for performance reasons. Filters can be applied hundreds and even thousands of times on each page load, so even micro performance is important.

A good place to start would be to add an is_callable() check and then run some benchmarks to see if it impacts performance.

#2 @SergeyBiryukov
16 months ago

  • Component changed from General to Plugins

#3 @Compute
16 months ago

Maybe just check for is_callable() when WP_DEBUG is turned on if it shows that performance is an issue?

Note: See TracTickets for help on using tickets.