Error when I put custom function in admin.php but all is fine if I put the code in functions.php

Hello everyone,

I would like to add a function in my code. When I add it to “admin.php” or “setup.php”, I get an error

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘myplugin_settings’ not found or invalid function name

If I put my function in “functions.php”, it works.

Is there a way to put it in one of the files mentioned above?

Thank you

function myplugin_settings() {
   // Add tag metabox to page
   register_taxonomy_for_object_type('post_tag', 'page');
   // Add category metabox to page
   register_taxonomy_for_object_type('category', 'page');
   }
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );

From the docs…

The PHP code in Sage is namespaced, so make sure to use namespaced functions and classes.

try…

add_action( 'init' , __NAMESPACE__ . '\\myplugin_settings' );

4 Likes