-1

To register my scripts i have this code in my functions.php

function enqueue_scripts() {
          wp_deregister_script('jquery');
          wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
          wp_register_script('flexslider', get_stylesheet_directory_uri() . '/js/flexslider.js', 'jquery', '', TRUE);

          wp_enqueue_script('jquery');
          wp_enqueue_script('flexslider');
}

add_action('wp_enqueue_scripts','enqueue_scripts');

The problem is when i delete these lines:

wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');

the site ignores the jQuery that was registered from wordpress. But in the source code the script is shown.

Does anyone have an idea?

5
  • What is your error, and what exactly do you want to achieve?
    – fischi
    Mar 4, 2014 at 11:17
  • The error is that wordpress ignores the jQuery script.
    – Nepo Znat
    Mar 4, 2014 at 11:23
  • What do you mean by "But in the source code the script is shown."? Does jQuery get delivered?
    – fischi
    Mar 4, 2014 at 11:26
  • I mean that "jquery" is present in the header of the page, but it does not work.
    – Nepo Znat
    Mar 4, 2014 at 11:31
  • Can you show some more code and Debug information? It is impossible to answer the question without it.
    – fischi
    Mar 4, 2014 at 11:40

1 Answer 1

1

Wordpress uses no conflict mode for jQuery but not when you use a cdn link to load jquery file.

You should use it something like this:

add_action( 'wp_enqueue_scripts', 'load_custom_jquery');
function load_custom_jquery(){
    wp_enqueue_script( 'custom_admin_jquery', get_stylesheet_directory_uri().'/js/flexslider.js', 'jquery' );
}

Just that much is enough. No other registering, deregistering will be required. This code will load your custom-jquery.

In wp_enqueue_script last parameter is jquery. which ensures that before loading custom-jquery load jQuery file (available in wordpress installation). That's the non conflicting mode. It is advisable that one should use this way only. Otherwise you might get conflicts with default jQuery file.

One more thing that you should consider while writing your jQuery code in custom_jquery.js is Aliasing the jQuery Namespace and use:

jQuery( document ).ready(function( $ ) {
  // Code using $ as usual goes here.
});

in place of

$( document ).ready(function() {
  //CODE
});

References :

jQuery no conflict

jQuery Ready Aliasing the jQuery Namespace SECTION

2
  • Thank you very much. I just added this: jQuery( document ).ready(function( $ ) { ... }); - and now it works.
    – Nepo Znat
    Mar 4, 2014 at 12:23
  • Glad it worked :) Mar 4, 2014 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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