Using require_once() within admin

In my theme directory I have the following structure:

/includes/functions-custom.php
/includes/avatax/AvaTax.php
/includes/classes/gmaps.class.php

I’m making a call from the functions-custom.php to include the second file within the WordPress admin using this code

require_once('avatax/AvaTax.php'); //Avalara Sales Tax API
if ( is_admin() ) {
    require_once('classes/gmaps.class.php');
}

The first require applies to both the theme and to the admin. The second, obviously, only applies to the admin. However, the first require fails with the following message:

Warning: require_once(/Users/philipdowner/Sites/dms/wp-content/themes/dms/includes/avatax/classes/BatchSvc/WP_User_Search.class.php) [function.require-once]: failed to open stream: No such file or directory in /Users/philipdowner/Sites/dms/wp-content/themes/dms/includes/avatax/AvaTax.php on line 23

Fatal error: require_once() [function.require]: Failed opening required '/Users/philipdowner/Sites/dms/wp-content/themes/dms/includes/avatax/classes/BatchSvc/WP_User_Search.class.php' (include_path=".:/Applications/MAMP/bin/php5/lib/php") in /Users/philipdowner/Sites/dms/wp-content/themes/dms/includes/avatax/AvaTax.php on line 23

I don’t feel like this should be such a stumper. Perhaps I’m overlooking something obvious? I’ve tried preceding the file path with the constant ‘TEMPLATEPATH’ and using the obvious WordPress functions like

require_once(get_template_directory().'/path/to/file');

I’m developing locally on MAMP. Particularly odd is the call to the file (in the error message) ‘/classes/BatchSvc/WP_User_Search.class.php’.

Can anyone help?

4 Answers
4

Avoid native php functions

  1. They are not relative, so breaking stuff is easy. (like in the OPs case)
  2. Why role your own if core got a function for you?
  3. They don’t consider edge cases

You should NEVER use the constants

  • STYLESHEET_DIRECTORY
  • TEMPLATE_DIRECTORY
  • WP_PLUGIN_DIR
  • etc…

Remember: Those are not API! and can therefore change!

but INSTEAD USE the following API functions:

  • Parent Theme: get_template_directory() / get_template_directory_uri()
  • Child Theme: get_stylesheet_directory() / get_stylesheet_directory_uri()

You can use both in Plugins and Themes:

  • plugin_dir_path(__FILE__).'/your/path/to/file.ext *) for Themes also
  • plugin_dir_url( __FILE__ ).'/your/path/to/file.ext

but plugin_dir_url() is reserved only for Plugins.

Keep in mind…

That there’re

  • Plugins
  • MU-Plugins
  • Drop-Ins
  • Themes
  • Themes in directories registered via register_theme_directory( $dir );

So please stick with the native API functions like wp_upload_dir();, content_url();, etc.

In general: Take a look at /wp-includes/link-template.php for a whole bunch of nice path and URl related API functions.

Leave a Comment