Title: STYLESHEETPATH in Plugin
Last modified: August 19, 2016

---

# STYLESHEETPATH in Plugin

 *  Resolved [Devin Price](https://wordpress.org/support/users/downstairsdev/)
 * (@downstairsdev)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/)
 * I’m working on a plugin that needs to check in the stylesheet directory for a
   file, if it doesn’t exist there, the template directory, and if its not there,
   it defaults to the plugin’s directory.
 * This seems like it should be simple, but for some reason I’m having trouble making
   it work.
 * Here’s the code:
 *     ```
       /* Loads the options array from the theme */
   
       if (file_exists( STYLESHEETPATH . '/functions.php' ) ) {
       	require_once STYLESHEETPATH . '/options.php';
       }
       else if (file_exists( dirname( __FILE__ ) . '/options.php' ) ) {
       	require_once dirname( __FILE__ ) . '/options.php';
       }
       ```
   
 * Also, please let me know if this can be done in a more elegant way. Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903703)
 * Try get_stylesheet_directory() – or better yet, get_template_directory(), which
   will preserve functionality when a Child Theme is in use.
 *  Thread Starter [Devin Price](https://wordpress.org/support/users/downstairsdev/)
 * (@downstairsdev)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903706)
 * 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](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';
       }
       ```
   
 *  [Matt Wiebe](https://wordpress.org/support/users/mattwiebe/)
 * (@mattwiebe)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903719)
 * 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';
       }
       ```
   
 *  Thread Starter [Devin Price](https://wordpress.org/support/users/downstairsdev/)
 * (@downstairsdev)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903828)
 * Okay, that makes sense. Thanks for the code!
 *  Thread Starter [Devin Price](https://wordpress.org/support/users/downstairsdev/)
 * (@downstairsdev)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903831)
 * 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';
       }
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘STYLESHEETPATH in Plugin’ is closed to new replies.

 * 5 replies
 * 3 participants
 * Last reply from: [Devin Price](https://wordpress.org/support/users/downstairsdev/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/stylesheetpath-in-plugin/#post-1903831)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
