• I hear so many conflicting answers to this question. When people suggest adding a small snippet of code to the functions.php file, it never works for me. I always end up getting errors or JQuery simply doesn’t work.

    Right now, I am adding it through scripts in my header. But, I hear this isn’t the correct way to do it.

    So, what is the simplest, correct way to enable JQuery?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter dwsimms

    (@dwsimms)

    That is how I am doing it. It occasionally causes JQuery conflicts, and I often read that this isn’t the proper way to enable JQuery.

    Thread Starter dwsimms

    (@dwsimms)

    And, I’m afraid I may a bit too stupid to understand the noConflict link you posted. Can you explain that a bit better? How exactly is that code used? What would the final code look like?

    Moderator keesiemeijer

    (@keesiemeijer)

    Where did you read that? This is the recommended method of linking JavaScript to a WordPress generated page.

    If other theme’s or plugins are loading their own jquery it will most of the time cause conflicts.

    Thread Starter dwsimms

    (@dwsimms)

    On a few of these threads. I’d have to do some searching. But, I’ve seen it a number of times. What should be in the functions.php, as far as Jquery is concerned? Should anything related to JQuery be in the functions.php?

    Thread Starter dwsimms

    (@dwsimms)

    If other theme’s or plugins are loading their own jquery it will most of the time cause conflicts.

    Any way to stop it?

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you explain that a bit better?

    The “$” shortcut is no longer available if you use wp_enqueue_script. Write “jQuery” each time you would normally write “$”.

    Lets say you have this script and want to use it with WordPress:

    $("button").click(function () {
    $("p").hide("slow");
    });

    You’ll need to rewrite it to:

    jQuery("button").click(function () {
    jQuery("p").hide("slow");
    });

    Or use your code in a wrapper as outlined in the wp_enqueue_script page:

    jQuery(document).ready(function($) {
    
    $("button").click(function () {
    $("p").hide("slow");
    });
    
    });

    What should be in the functions.php, as far as Jquery is concerned? Should anything related to JQuery be in the functions.php?

    Only the wp_enqueue_script should be in your theme’s functions.php. This will load your script (file) and if you used the $deps argument (array( ‘jquery’ )) jQuery will be loaded before it.

    Any way to stop it?

    Ask the plugin or theme developer to use the correct way of using javascript with WordPress or use a different plugin or theme.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘What is the proper way to enable JQuery in WordPress’ is closed to new replies.