How do I add a simple jQuery script to WordPress?

I know what you mean about the tutorials. Here’s how I do it:

First you need to write your script. In your theme folder create a folder called something like ‘js’. Create a file in that folder for your javascript. E.g. your-script.js. Add your jQuery script to that file (you don’t need  <script> tags in a .js file).

Here is an example of how your jQuery script (in wp-content/themes/your-theme/js/your-scrript.js) might look:

Notice that I use jQuery and not $ at the start of the function.

Ok, now open your theme’s functions.php file. You’ll want to use the  wp_enqueue_script() function so that you can add your script whilst also telling WordPress that it relies on jQuery. Here’s how to do that:

Assuming that your theme has wp_head and wp_footer in the right places, this should work. Let me know if you need any more help.

Add a Comment

Your email address will not be published. Required fields are marked *