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?
How about using
locate_template()
for the inclusion andwp_enqueue_script()
for the scripts? There’s alsocontent_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 🙂
try
as a way of including that file rather than the straight include.
What about changing the path for
get_template_directory_uri()
like this: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 yourtracking.php
would look like this: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:As I said, it’s not necessary as jQuery should be included by default, but who knows.
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 yourtracker.php
file (or theheader.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: