Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building WordPress Widgets, and Shortcodes Adding AJAX To Plugins on the Front-End: Part 2

Michael Hanna
Michael Hanna
17,649 Points

Uncaught ReferenceError: jQuery is not defined

I get this error in the console when refreshing my page:

Uncaught ReferenceError: jQuery is not defined

with a link to line 1 of my javascript file, which is identical to the one in the project downloads and the one in the video (as far as I can tell).

jQuery(document).ready(function($){

    $('.wptreehouse-badge').hover(function() {
        $(this).find('.wptreehouse-badge-info').stop(true, true).fadeIn(200);
    }, function() {
        $(this).find('.wptreehouse-badge-info').stop(true, true).fadeOut(200);
    });


    $.post(ajaxurl, {

        action: 'wptreehouse_badges_refresh_profile'

    }, function( response ) {

        console.log( 'AJAX complete' );

    });

});

From googling the error it sounds like I need to include jQuery somehow...but other posts say jQuery is already included in WordPress (I'm using WP 4.4.2)

1 Answer

Michael Hanna
Michael Hanna
17,649 Points

Oh. I figured it out. I'll post it here, though, in case someone else has the same problem.

I had to add array('jquery') as a third argument in my enqueue_script function in the main plugin file. Zac's file doesn't include that third argument, so maybe it's something unique to my setup or version of wordpress. But my code now looks like this:

<?php

function wptreehouse_badges_frontend_scripts_and_styles() {

    wp_enqueue_style( 'wptreehouse_badges_frontend_css', plugins_url( 'wptreehouse-badges/wptreehouse-badges.css' ) );
    wp_enqueue_script( 'wptreehouse_badges_frontend_js', plugins_url( 'wptreehouse-badges/wptreehouse-badges.js' ), array('jquery') );

}
add_action( 'wp_enqueue_scripts', 'wptreehouse_badges_frontend_scripts_and_styles');

?>

Sorry to answer my own question...hopefully this is helpful to someone in the future.