WordPress.org

Ready to get started?Download WordPress

Forums

What is the proper way to enable JQuery in WordPress (8 posts)

  1. dwsimms
    Member
    Posted 11 months ago #

    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?

  2. keesiemeijer
    moderator
    Posted 11 months ago #

  3. dwsimms
    Member
    Posted 11 months ago #

    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.

  4. dwsimms
    Member
    Posted 11 months ago #

    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?

  5. keesiemeijer
    moderator
    Posted 11 months ago #

    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.

  6. dwsimms
    Member
    Posted 11 months ago #

    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?

  7. dwsimms
    Member
    Posted 11 months ago #

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

    Any way to stop it?

  8. keesiemeijer
    moderator
    Posted 11 months ago #

    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.

Reply

You must log in to post.

About this Topic