Support » Developing with WordPress » I do not understand how to use jQuery with WordPress

  • I’ve read all the documentation on the WordPress site multiple times, read dozens of tutorials, watched videos on Youtube, and tried various plugins, and I cannot figure out how to use jQuery with WordPress. I get that it’s included with WordPress but has to be “enqueued.” I sort of understand what that means.

    I’ve got a rough version of what I am trying to do working in JsFiddle: https://jsfiddle.net/m388uxho/1/

    I don’t get how to get this into WordPress. I have tried placing the code on the WordPress enqueue script Code Reference page in functions.php, in the header, and in the footer: wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

    Can someone please take me to square one on how to “unlock” jQuery inside WordPress, and how I can actually implement this simple script on my page as I did in JsFiddle? I would be extremely grateful. Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • When you use wp_enqueue_script you need to add it using the appropriate action. You can’t just cut-and-paste the code that you’ve got above in anywhere as that’s information, not an actual bit of code.

    The way that it works woudl be like this:

    add_action ( 'wp_enqueue_scripts', 'add_my_scripts' );
    
    function add_my_scripts () {
        wp_enqueue_script ('my_script', 'http://site.com/js/my_script.js', array ('jquery') );
    }

    Remember that you’ll have to update what I’ve given above to suit your own script files location and name.

    Thank you. I don’t understand is where what you’ve posted goes (or at least the customized version), and what I need to use to call up this code. Am I supposed to post this on each page I need to use it on?

    It would nomrally go in your themes (preferably child themes) functions.php file.

    That will include your JavaScript file on every page of the site.

    If you want an easy way of including JavaScript/jQuery, take a look at the Header and Footer plugin. This plugin allows you to add your own scripting code to all of your pages, just make sure to bracket your code with <script> ... </script> tags. Then you don’t have to worry about creating a child theme.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘I do not understand how to use jQuery with WordPress’ is closed to new replies.