Using /lib/extras.php

Hey guys,

I have a quick question about the /lib/extras.php.
I understand that I can put here my custom hooked functions, this works, but only sometimes.

It seem that some filter/action hook aren’t working if they’re used from extras.php. They only work from the themefunction.php.

The throwed error is like below :
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'woo_add_to_cart_check_custom' not found or invalid function name in ...\project\wp-includes\plugin.php on line 235

Is there something i didnt understood about this file ? Or is it used but for some special hooks ?

Thanks :smile:

/lib/extras.php is namespaced. You must include namespace when passing a function within it as a callback.

See “Namespaces” section: https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/

3 Likes

Thanks master. It was the perfect answer.

Last question, they said on the page you sent me that if for example i need to use wp_query() i need to precise root namespace (or throught “use” statement).
But it seem i dont need it to use the wordpress get_option() function.

How do we know wich function (WP core, i think that plugin function always need namespacing) need to have the root namespace specified ?

Thanks again, a lot really.

You only need to worry about classes, functions automatically are assumed to be in the global namespace, unless you prefix a function with a namespace

1 Like

Thanks @kalenjohnson, all is good now :smile: