WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] STYLESHEETPATH in Plugin (6 posts)

  1. Devin Price
    Member
    Posted 3 years ago #

    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.

  2. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Try get_stylesheet_directory() - or better yet, get_template_directory(), which will preserve functionality when a Child Theme is in use.

  3. Devin Price
    Member
    Posted 3 years ago #

    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';
    }
  4. Matt Wiebe
    Member
    Posted 3 years ago #

    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';
    }
  5. Devin Price
    Member
    Posted 3 years ago #

    Okay, that makes sense. Thanks for the code!

  6. Devin Price
    Member
    Posted 3 years ago #

    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';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.