I’m having a bit of trouble understanding how to put javascript into a wordpress theme.
- I know I have to save this javascript:
http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
and put it in a folder on the server under “js” but what do I do with the functions javascript?
$(document).ready(function(){
$('#contactButton').click(function(){
$('#contactDropDown').show("fast");
$('#contactDropDown').animate({height:'500px'}, 200);
});
$('#closeBox').click(function(){
$('#contactDropDown').hide("fast");
$('#contactDropDown').animate({height:'0px'}, 200);
});
});
Do I save this as a different document but save it to the same “js” folder?
- What is the exact code that I need to put into the functions.php of the theme?
i also created a fiddle for a little more understanding: http://jsfiddle.net/ptemyw52/
(the javascript is for the drop down contact box)
You use the WordPress
wp_enqueue_script
function to tell WordPress what to load.It has jQuery builtin, so you can just tell it:
to get that. For your own code, put it in a file somewhere (e.g. js/scripts.js) and then tell WordPress to load it with:
The final argument
array('jquery')
tells WP that your scripts depend on having jquery.WordPress Already has JQuery included.
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
You have to make sure is it enqued somewhere. If JQuery is not running either in your functions.php or your head.php enqueue it like so:
WordPress loads JQuery in no-conflict mode so the bling ($) is not going to work you need to atually type in the jQuery. You can actually just type it out and pass it in through your document ready, like so:
You can actually just add this to your footer.php if this the only code you have.. If you end up having more javascript you might want to make it’s own file and enqueue as well.
in: wp-content/themes/your-template/js you put your js file.
Then in head of document (it could be header, index etc.)