Error when adding functions in extras.php

When trying to create a Custom post type, placing standard code in extras.php

I get this error:

PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, function 'my_custom_post_type' not found or invalid function name in /path_wp/wp-includes/plugin.php on line 496

Instead, If I place the code inside functions.php it works.
Notice that this procedure always worked with the old Roots theme.
It’s the same when try to adding my taxonomies.

How to solve it?

You’ll need to read up on namespaces: http://php.net/manual/en/language.namespaces.php

Once you’ve done that, you’ll know to take into account the current namespace when calling filters or actions:

add_filter('some_filter',  __NAMESPACE__ . '\\some_function_to_filter_some_filter');
4 Likes

Solved. Thanks…

Oops. Another similar problem I’m having is that I can’t call functions.
I added in extras.php a simple function like this:

function hello_man() {
  return "hello man";
}

When I call it from a template I get this error:

PHP Fatal error: Call to undefined function hello_man() in …

This always worked before Sage.

[SOLUTION]

How to use namespaced functions and classes

You may notice that if you simply try to call <?= title(); ?> you’ll receive a Call to undefined function title() fatal error. This is because we now need to use the full namespace. In order to use the title() function, you will need to use it this way: <?= Branch\Titles\title(); ?>

This is covered in https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/ by @kalenjohnson.

See “How to use namespaced functions and classes”