Support » Themes and Templates » TEMPLATEPATH vs get_bloginfo('template_directory')

  • Hallo!
    Recently, building up a theme, I stumbled upon an error I could not easily solve.

    I was setting up few variables, relative to the path of php and js files, and I realized that I had to use different gergo, for different things. For instance:


    $styles_path = get_bloginfo('template_directory') . '/library/styles/';
    $theme_path = TEMPLATEPATH . '/library/theme/';

    I looked into this forum for an explanation about why sometimes I have to use TEMPLATEPATH and others get_bloginfo(‘template_directory’), but I could not find any posts on the topic.

    Does anyone know the difference between the two?

    Thanks in advance, ciao!
    Carlo

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi

    (@esmi)

    Forum Moderator

    Use:

    $styles_path = get_stylesheet_directory() . '/library/styles/';
    $theme_path = get_stylesheet_directory() . '/library/theme/';

    Thank you Esmi,
    but if I use get_stylesheet_directory() I get a 404 error.

    A note, right now I’m working on a local environment, and my purpose would be to have a method valid in local as well when I deploy the website online.

    esmi

    (@esmi)

    Forum Moderator

    That’s why you should use get_stylesheet functions. Try echoing out get_stylesheet_directory(). Perhaps your generated path is incorrect.

    Nope,
    the path is correct only if I use TEMPLATEPATH for $theme_path and get_bloginfo( ‘template_directory’ ) for $styles_path. This cos I need those variables to later load other files.

    If I use get_stylesheet_directory() I obtain an incorrect path.

    Now, the point here is to understand where TEMPLATEPATH, get_bloginfo( ‘template_directory’ ) (and get_stylesheet_directory() ) vary, since I need those variables to retrieve later files.

    Later I need to use

    include_once( $theme_path . 'theme-init.php' );

    …and later again

    print "<link rel='stylesheet/less' id='style-less-css'  href='" . $styles_path . "style.less' type='text/css' media='screen, projection' />\n";<br />
    print "<script type='text/javascript' src='" . $js_path . "less-1.1.3.min.js'></script>\n\n";

    So, TEMPLATEPATH works with include(), get_bloginfo() works later with print()… and I get confused.

    If I use TEMPLATEPATH to retrieve all the variables, I get broken links for style.less and the less.js. If I use get_bloginfo(‘template_directory’) or get_stylesheet_directory() for all of those, I get broken links for the stylesheet and the js file.

    In details, TEMPLATEPAH returns

    src='/Users/carlorizzante/Documents/myProjects/mamp/htdocs/mariakosh/wp-content/themes/mariakosh/library/js/less-1.1.3.min.js'

    …while get_bloginfo(‘template_directory’) returns

    src='http://localhost:8888/mariakosh/wp-content/themes/mariakosh/library/js/less-1.1.3.min.js'

    …and get_stylesheet_directory()

    src='/Users/carlorizzante/Documents/myProjects/mamp/htdocs/mariakosh/wp-content/themes/mariakosh/library/js/less-1.1.3.min.js'

    So, my question is, what’s exactly the difference between TEMPLATEPATH and other ways to retrieve paths to files?

    To be more precise, the following works:

    $theme_path = TEMPLATEPATH . '/library/theme/';
    include_once( $theme_path . 'theme-init.php' );

    …and…

    $js_path = get_bloginfo('template_directory') . '/library/js/';
    print "<script type='text/javascript' src='" . $js_path . "less-1.1.3.min.js'></script>\n\n";

    Hope this way is more clear.

    get_stylesheet_directory_uri();
    – returns the http:// path to either the child theme directory location or the main theme if no childtheme exists.

    get_stylesheet_directory();
    – returns the local directory equivalent location to the files either in the child theme if it exists or the main theme if no child theme

    get_template_directory_uri();
    – returns the http:// location of your main theme itself and never the child theme

    get_template_directory();
    – returns the local directory location of the main theme files

    The above post shows that you are in effect asking for two different things. The include_once should actually be:

    get_template_part(‘theme-init’);

    The $js_path = get_bloginfo(… should actually be:

    $js_path = get_stylesheet_directory_uri() . ‘/library/js/’;

    ^ The reason for these ways of using is that the theme review team wants the end user to have the ability to override the files if necessary in the child theme if child themes are created for your theme and utilizing TEMPLATEPATH is not guaranteed to find the right directory location if variables are set otherwise while the initiation of wordpress is executed.

    Thank you very much!

    So, if I’m on the right path, the code below should be just fine (and indeed it works smoothly)

    [Code moderated as per the Forum Rules. Please use the pastebin]

    My regards 🙂

    Merci

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘TEMPLATEPATH vs get_bloginfo('template_directory')’ is closed to new replies.