In my WordPress blog I included a javascript-based slideshow at the top which works well. I have a PHP file which outputs the necessary javascript that has to go in the <head>
tag by means of an "add_action" in my child theme's functions.php file:
function add_slideshow_js() {
include('/path/slideshow_output.php');
echo $js_output;
}
add_action( 'wp_head', 'add_slideshow_js' );
The javascript that this PHP outputs includes a call to the controlling javascript file:
<script src="path/slideshow.js" type="text/javascript"></script>
... followed by the javascript that makes the page's slideshow.
It all works very well, looks great. The thing is, it's dependent on jquery, but it was working fine, so I wondered how jquery was actually being included.
I tuned off all my plugins and it stopped working. So obviously one of them was including jquery and allowing it to work.
But I thought I'd like to make sure that jquery will be included, without relying on a plugin, since I might delete the vital plugin at some point in the future, and of course I'd still want the slideshow to work.
So, with all plugins disabled, I thought I'd include jquery explicitly myself, by writing this function in my child theme's functions.php file:
function insert_jquery(){
wp_enqueue_script('jquery');
}
add_filter('wp_head','insert_jquery');
Now, that does include jquery, but it's called in the footer, and by experimenting I find that the slideshow will only work if jquery is called inside the <head
> tag.
I could write a function to call my own jquery file, but I get the impression that it's best to use the jquery included with WordPress, and in any case, I thought that re-enabling the plugins would then get jquery called twice.
So what function can I write in my child theme's functions.php file that will reliably get the WordPress jquery loaded in the <head>
section of the page?
Hope this all makes sense.