Script will not print in head if path to file is correct

I’m dealing with a rather hackish implementation, where header.php has this line: include_once "tracking.php";. Inside tracking.php, there are these lines:

<script type="text/javascript" src="/wp-content/themes/themename/js/jquery-1.3.2.min.js"></script>
  <script type="text/javascript" src="/wp-content/themes/themename/js/jquery.cookie.js"></script>

However, as long as the paths to those .js files are correct, they don’t get printed in the page’s head. If I introduce a typo so that the path is incorrect, then they do get printed!

Read More

I realize this is not an optimal way of including these files. However, does anyone have an idea as to why they aren’t being printed if the path used is correct?

Related posts

Leave a Reply

4 comments

  1. How about using locate_template() for the inclusion and wp_enqueue_script() for the scripts? There’s also content_url() to target /wp-content/.

    Also: How did you inspect it? Sourcecode? FireBug? Try to open source in FF and then click the link. If you get a “file not found”, you know what it’s about, else it should be loaded. The fact that it really doesn’t get “printed” to the screen is something I never saw before (in the way) you described. Best solution would be the first paragraph of my A: Switch method of inclusion and stop worrying 🙂

  2. What about changing the path for get_template_directory_uri() like this:

    <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery-1.3.2.min.js"></script>
    

    You could also use TEMPLATEPATH to get the path, not the uri. But anyway, why don’t you just include those lines directly in the header?

    UPDATE

    If you like to use wp_enqueue_script you have to remember hook it to an action, so your tracking.php would look like this:

    function register_scripts() {
        wp_enqueue_script('jquery');
        wp_register_script('jquerycookie', get_template_directory_uri() . '/js/jquery-1.3.2.min.js', array('jquery'));
        wp_enqueue_script('jquerycookie');
    }
    add_action('wp_enqueue_scripts', 'register_scripts');
    

    That will register the new script (/js/jquery.cookie.js) and print it with the other scripts. I’m not sure if it’s really necessary to include jQuery as it should be already included, just to be sure you can add this at the beginning of the function:

    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
    

    As I said, it’s not necessary as jQuery should be included by default, but who knows.

  3. wp_enqueue_script() can handle the ability to include javascript instantly as well as check for dependancies.

    A simple php function: <?php wp_enqueue_script('jQuery') ?> in the your tracker.php file (or the header.php) will load jquery instantly.

    You then your other script <?php wp_enqueue_script('jquery_cookie', TEMPLATEPATH. 'js/jquery.cookie.js', 'jquery')

    And finally together:

        <?php 
        function mysite_init() {
            if (!is_admin()) { // only fire this on non-admin pages
            wp_enqueue_script('jquery');
            wp_enqueue_script('jquery_cookie', TEMPLATEPATH. 'js/jquery.cookie.js', 'jquery'); 
    }
        }
        // Add your hook to fix issue #11526 http://core.trac.wordpress.org/ticket/11526
        add_action ('init', 'mysite_init');
        ?>