Does the namespace always have to be prefixed to the function or class when referencing callbacks in WordPress, even when they are in the same file?
I want to use namespaces instead of prefixes for a plugin, since now the minimum supported PHP version for WP is 5.6.20 and namespaces can be used.
But it does not seem to work with hooks:
namespace My\Whatever;
// Enqueue CSS and JS
function enqueue_css_and_js() {
// Do it...
}
add_action( 'wp_enqueue_scripts', 'enqueue_css_and_js' );
Results in this error:
call_user_func_array() expects parameter 1 to be a valid callback, function 'enqueue_css_and_js' not found or invalid function name
However, if I prefix the function with the namespace:
add_action( 'wp_enqueue_scripts', 'My\Whatever\enqueue_css_and_js' );
It works as expected.
I wonder why this happens, since I am writing this inside the same PHP file.
And the real question is:
Should I always prefix all my functions with the namespace when referencing callbacks in WordPress, or does this only happen in certain cases with certain hooks?
By referencing callbacks, I mean all of them, not only for actions and filters, but also for the callbacks in add_settings_section(), add_meta_box(), etc...