Fatal error: Call to undefined function add_action() – an untouched problem

The question:

The question was asked many times in many ways, but unfortunately is not covered well in most of the answers. I’m having the same issue doing things using include-files.

In my TwentyFourteen child theme’s functions.php I used:

require_once get_stylesheet_directory_uri() .'/admin-panel/theme-options.php';

And I’m using Ian Stewart’s Sample Theme Options. I’ve used it in many of my projects, the same way mentioned here, without having the error. But recently I’m having the issue in several of my projects. If I take the code to functions.php then everything’s fine. But it’s occurring only when I’m trying to include/require external file.

I’m having the error:

Fatal error: Call to undefined function add_action() in C:xampphtdocsprojectwp-contentthemesmy-themeadmin-paneltheme-options.php on line 2

In this case I’m having the error using a child theme (of T14). But even in one of my parent theme I’m facing the issue. But in the same environment my other projects are running well with all the includes and/or requires (where several add_action()s are defined). I’m having issues in ALL the add_action() calls, like CPT assigning, Custom Taxonomy assigning etc. But I’m successfully developing another two projects including external files from inc/ folder where all my CPTs are assigned and their meta boxes too.

I tried reinstalling WordPress, deactivating all my plugins, tried from Child Theme, tried only the same thing by modifying TwentyFifteen (applying default theme), tried require(), require_once(), include(), include, require_once, and require one after another with failure. And I’m using WP 4.1.1 in all the good and problematic installs.

Majority of the previous questions are answered with a simple line:

As the message says, you haven defined the function add_action() and yet you’re trying to use it.


  • how come (isn’t it already defined by WordPress)? and
  • why it’s differing project to project in the same environment (How come it’s out of the scope now only in particular project[s])?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

get_stylesheet_directory_uri() returns an URL. The requested file is called like any other publicly available resource: without the context of the include statement. This is like opening the PHP file in a browser directly. There is no WordPress context, and the functions add_action() or add_filter() are not defined.

Use get_template_directory() in parent themes and get_stylesheet_directory() in child themes.

Disable allow_url_fopen and allow_url_include in your development environment to catch errors like this early and with better error messages.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment