1

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!

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?

4 Answers 4

2

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
  • Same result if I do it the 'correct' way - nothing gets printed to the screen (viewed with Firebug, Chrome inspector, viewing the page source, etc.) as long as the path is correct. If it's incorrect, then I have no trouble getting it to show up in source. That doesn't really accomplish anything, though ;). Jul 27, 2011 at 15:47
  • hm... sounds extremly strange. Have you tried putting it into a function inside functions.php? Then adding it to wp_head() maybe helps
    – kaiser
    Jul 27, 2011 at 18:26
1

try

get_template_part('tracking');

as a way of including that file rather than the straight include.

0

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.

2
  • Small note - wp_enqueue_scripts hook should be used for enqueue, not wp_print_scripts. Also you don't need to queue jQuery explicitly if you queue another script that has it listed as dependency.
    – Rarst
    Jul 27, 2011 at 15:43
  • Thanks, didn't noticed about wp_enqueue_scripts, and yes, I had the feeling that the jQuery queue was too much. Still I'll leave it on the answer.
    – Mario
    Jul 27, 2011 at 16:08
0

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');
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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