Using Includes in Templates in Document Head

Including PHP files inside actions isn’t recommended, and is considered highly unusual. If what you were trying to do worked, it would be fragile, and heavily dependent on ordering.

The reason your code didn’t work:

add_action('wp_head', 'fst_include',20);

Is because actions are added with a priority of 10 by default, yet your include is on priority 20, so it happens after the other hooks, not before. As a result, when more_css is executed, it has yet to be defined.

Instead, don’t load the file on a hook, just load it immediatley so that the functions are defined, but not executed, then add the functions as actions.

In general, it is a bad idea to mix loading code, and running code. Try to:

  • Load the code or autoloader
  • Create all the objects but don’t run them
  • Then run it once everything is assembled

Leave a Comment