Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to understand the difference between:

<?php bloginfo( 'stylesheet_url' ); ?>

and

<?php bloginfo('template_url'); ?>/style.css

because I think the second one is more flexible (is i decide to move the file in a subdirectory, for example. or if I have more stylesheet to add.. the second one seems smarter). Am I wrong?

In particular, where is 'stylesheet_url' stored? and how to change it?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

bloginfo('stylesheet_url') will always return the current theme's stylesheet. If current theme is a child theme, for example, it will return style.css relative to the child theme's root rather than the parent theme's root.

bloginfo('template_url') will always return the parent theme's template directory URL. So using <?php bloginfo('template_url'); ?>/style.css will always be the parent theme's stylesheet.

Since bloginfo echo's things out, I'm guessing you're not using the WordPress enqueue system, which you should be. That is the most flexible. An end user wants to remove your stylesheet? Simple Just dequeue it.

That said, you can change the output of bloginfo via a filter.

To understand how this works you have to trace how bloginfo works. To start, bloginfo is a wrapper for get_bloginfo. So let's take a look at that, the line relative to your question:

<?php
function get_bloginfo( $show = '', $filter = 'raw' ) {

    switch( $show ) {
        // snip snip

        case 'stylesheet_url':
            $output = get_stylesheet_uri();
            break;
        case 'stylesheet_directory':
            $output = get_stylesheet_directory_uri();
            break;
        case 'template_directory':
        case 'template_url':
            $output = get_template_directory_uri();
            break;

                // snip snip
    }

    $url = true;
    if (strpos($show, 'url') === false &&
        strpos($show, 'directory') === false &&
        strpos($show, 'home') === false)
        $url = false;

    if ( 'display' == $filter ) {
        if ( $url )
            $output = apply_filters('bloginfo_url', $output, $show);
        else
            $output = apply_filters('bloginfo', $output, $show);
    }

    return $output;
}

And here's bloginfo.

<?php
function bloginfo( $show='' ) {
    echo get_bloginfo( $show, 'display' );
}

As you can see, the filter is always set to display, which means WP will use apply_filters on get_bloginfo's output.

So to change things, you hook into either bloginfo or bloginfo_url. We want bloginfo_url.

<?php
add_filter('bloginfo_url', 'wpse76262_change_stylesheet', 10, 2);
function wpse76262_change_stylesheet($url, $show)
{
    if ('stylesheet_url' == $show) {
        $url = '/the/change/stylesheet.css';
    }

    return $url;
}

There are also filters in get_stylesheet_uri, get_template_directory_uri, and get_stylesheet_directory_uri that you could use as well.

share|improve this answer
add comment

There are two differences between both methods:

  1. 'stylesheet_url' refers always to the directory of the current theme, be it a child theme or a parent theme. 'template_url' is always just the parent theme.
  2. 'stylesheet_url' can be filtered by a plugin or a child theme. The filter is named 'stylesheet_uri'. The combination of 'template_url' and an appended string cannot be filtered. It is less flexible.

If you want to keep it flexible enqueue the stylesheet. Do not write a link element into the theme’s header.php.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.