Try get_stylesheet_directory() – or better yet, get_template_directory(), which will preserve functionality when a Child Theme is in use.
Thanks Chip, it works using those tags. But I’m still curious why TEMPLATEPATH and STYELESHEETPATH aren’t working. I thought they were defined constants.
(And I realize there was a typo above, they should all be options.php, just had functions.php in there for testing)
From Tadlock’s post (http://justintadlock.com/archives/2010/11/17/how-to-load-files-within-wordpress-themes):
get_template_directory(): Returns the absolute template directory path. It returns the same value as the TEMPLATEPATH constant.
The working code looks like this:
/* Loads the options array from the theme */
if (file_exists( get_stylesheet_directory() . '/options.php' ) ) {
require_once get_stylesheet_directory() . '/options.php';
}
else if (file_exists( get_template_directory() . '/options.php' ) ) {
require_once get_template_directory() . '/options.php';
}
else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
require_once dirname( __FILE__ ) . '/options.php';
}
If you’re calling for STYLESHEETPATH in your plugin as soon as it’s loaded, the issue is simple: STYLESHEETPATH isn’t defined yet. It’s reliably available by the time the “init” hook fires.
If you set your code to run after init, you might want to use WP’s built-in locate_template() function (which is everywhere used internally by WP for loading parent/child templates) like the following:
if ( $options = locate_template( array('options.php') ) {
require_once($options);
}
else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
require_once dirname( __FILE__ ) . '/options.php';
}
or, if you wanted to get fancy and terse (note the second argument to locate_template):
if ( ! locate_template( array('options.php'), true) && file_exists( dirname( __FILE__ ) . '/options.php' ) {
require_once dirname( __FILE__ ) . '/options.php';
}
Okay, that makes sense. Thanks for the code!
The above snippet was one ‘)’ off, for anyone else who’s looking to use this:
if ( $optionsfile = locate_template( array('options.php') ) ) {
require_once($optionsfile);
}
else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
require_once dirname( __FILE__ ) . '/options.php';
}