I have a class file and I have separated out my custom post types into their own files in a sub-folder for neatness, readability, etc.

I want to init the CPT now in my plugin's index.php class. My class is initialised on the plugins_loaded action and in my construct I have the following:

public function __construct() {
    add_action('admin_enqueue_scripts', array($this,'init_admin_scripts'));
    add_action('wp_enqueue_scripts', array($this,'init_frontend_scripts'));

The above actions refer to functions that are in the index.php file itself. My CPTs are in a subfolder called cpt. What is the correct way to add the action for the functions in a subfolder?

  • 2
    Why not use include or require before calling those functions?
    – Johansson
    Aug 23, 2018 at 12:02

1 Answer 1


Adding an action doesn't do anything except putting the callback function in the queue of the corresponding hook. It doesn't check if the function exists, let alone load a file where the function is resident.

Only when the corresponding hook is fired PHP looks for the function. If it is not loaded by then it simply throws an error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'yada_yada' not found or invalid function name in /../wp-includes/class-wp-hook.php on line 286

So, you are yourself responsible for loading the file where the function resides using include or require before the admin_enqueue_scripts hook is fired. Refer to the hook order list to check if you're hooking timely. If you're hooking everything into plugins_loaded you should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.