0

I have a WordPress theme that includes jQuery, but for some reason the "$" doesn't let me call jQuery. I know I can wrap jQuery calls in an anonymous function, but I have other plugins that rely on the $ working, and I don't want to modify their code.

So I need a way to make "$" work for jQuery, globally, across all JS in my site.

My theme adds jQuery with this (in functions.php):

function ss_scripts() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-ui-core');
    wp_enqueue_style( 'ss-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'ss_scripts' );

So two questions:

  1. Why doesn't jQuery default to the "$" when the script is added this way?
  2. How can I make the $ use jQuery, globally, across my entire site?
1

Why doesn't jQuery default to the "$" when the script is added this way?

The $ alias has been chosen by several different libraries an alias. This causes conflict if you try to use more than one library. It is a bit ridiculous, actually-- like several major corporations all deciding to use the same phone number.

How can I make the $ use jQuery, globally, across my entire site?

WordPress load jQuery in noConflict mode to avoid the problems caused by the above foolish aliasing. You should not reintroduce those issues.

1

To use $ as the object reference, you need to do this after jQuery is included and before any other script that uses $:

var $ = jQuery.noConflict();

Use at your own risk, may conflict with other code.

Technically, any jQuery plugin that relies on $ is written incorrectly.

0

Instead of $ use 'jQuery'. I am not sure about your WordPress version. For the updates with jQuery. 1. 9. It's better to use jQuery instead of $.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.