WordPress.org

Support

Support » Plugins and Hacks » [Resolved] STYLESHEETPATH in Plugin

[Resolved] 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)
  • 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';
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] STYLESHEETPATH in Plugin’ is closed to new replies.
Skip to toolbar