1

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:\xampp\htdocs\project\wp-content\themes\my-theme\admin-panel\theme-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.

But,

  • 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])?
  • you know it is too local or it is a php question to get an answer here... – Mark Kaplun Feb 22 '15 at 7:28
  • @MarkKaplun is it actually? If so I'd love to have this migrated to SO, but it's conflicting only with add_action() - a WP Core function. – Mayeenul Islam Feb 22 '15 at 7:38
  • 2
    get_stylesheet_directory_uri() is an URL, so the file is called outside of the WP context. Hence no add_action() or other WP functions. You need a path. – fuxia Feb 22 '15 at 7:55
  • @toscho bullseye! 8) That is the answer. I'm afraid I messed up get_..._dir_uri() with get_..._dir(). get_stylesheet_directory() solved the problem. Would you be kind to answer the question with this? It'll help others having the same issue. – Mayeenul Islam Feb 22 '15 at 8:02
  • @MayeenulIslam, hmm I assumed that you open the file and not the url ;) – Mark Kaplun Feb 22 '15 at 8:16
3

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.

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.