Dmitri
Dmitri

Reputation: 579

$ is undefined error in WordPress when including Graph Library for Jquery

I included a Graphing and Plotting Jquery library called JQplot with wordpress using the following code in my functions.php file:

function rw_jqplot() {
    // JS
    wp_deregister_script('jqplot');
    wp_enqueue_script('jqplot', plugins_url('/js/jquery.jqplot.min.js', __FILE__), array("jquery"), '1.0.0.1012');
    // CSS
    wp_deregister_style('jqplot');
    wp_enqueue_style('jqplot', plugins_url('/css/jquery.jqplot.min.css', __FILE__), false, '1.0.0.1012');

} add_action('init', 'rw_jqplot'); ?>

The problem is that I get an error that says (in firebug): $ is undefined.

I did some research and I think that it has to do something with jquery running in no conflict mode. So maybe a solution to this is to somehow include the above code within the wordpress no conflict mode? is that possible to do?

Thanks!

Upvotes: 2

Views: 967

Answers (1)

ldiqual
ldiqual

Reputation: 15375

Looking at the JQPlot code, the whole plugin is declared within a closure:

(function($) { 
  // JQPlot code
})(jQuery);

So the plugin is fine ($ = jQuery in the plugin scope). However, the effects declaration is not:

var backCompat = $.uiBackCompat !== false;
// And then, JQPlot effects declaration

That's an issue, and you should report it to the development team. For a quick&dirty fix, you can add (function($) { at the line 10577 and })(jQuery); at the end of the jquery.jqplot.js.

Edit

The latest stable version (0.9.7r635) seems not to have this issue. Maybe give it a try !

Upvotes: 3

Related Questions