2

I'm having a bit of trouble understanding how to put javascript into a wordpress theme.

  1. 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?

  1. 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)

3 Answers 3

3

You use the WordPress wp_enqueue_script function to tell WordPress what to load.

It has jQuery builtin, so you can just tell it:

wp_enqueue_script('jquery');

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:

wp_enqueue_script(
    'myscript',
     get_stylesheet_directory_uri() . '/js/scripts.js',
     array('jquery')
);

The final argument array('jquery') tells WP that your scripts depend on having jquery.

Sign up to request clarification or add additional context in comments.

10 Comments

If what you show in the question is what you need, then you can put it directly in the JS file as-is. WordPress will load in jQuery and then load in your script. You script will then run and you should be set.
do I need to put my functions code in <script> when I save it?
No, you do not need script tags. Just put your code in the file as-is.
and I add this enqueue script into the functions.php right? because when I do and change the link to my file it comes up with an error
What is the error? And make sure that the url is right. get_stylesheet_uri() returns the URL of your theme directory. The example script I gave above assumes that your script file is in: example.com/wp-content/themes/yourtheme/js/scripts.js. If your script is somewhere else, you would need to change that.
|
1

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:

wp_enqueue_script('jquery');

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:

<script>
jQuery(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);
     });
});
</script>

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.

6 Comments

do i have to put that code in <script> ? why would i put it in the footer?
yeah put in in the <script></script> tags
okay did saved the functions with the script tags and added it into the js folder, when i enqueue it with the link @bobdye suggested, it comes up with an error so I'm having trouble eunqueueing it
Can you paste the exact error here so I can see it?
i actually got rid of the error! but unfortunately the javascript isn't working on my website
|
-1

in: wp-content/themes/your-template/js you put your js file. Then in head of document (it could be header, index etc.)

<script src="<?php echo get_template_directory_uri(); ?>/js/your.js"></script>

3 Comments

Do not do this... This is really bad practice. This is not how you load scripts in wordpress
do you have a suggestion of how to do this @w3bMak3r?
I posted an answer below. Wordpress uses enqueue scripts to load the scripts in the right places

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.