0

To avoid multiple jQuery calling from my theme and plugins, I removed <script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts/jquery.js"></script> .
Now my slider plugin starts working fine in the front end. I don't know whether it will it cause any problems to my website in future ?
Any suggestions.

4
  • 1
    jQuery is included in WordPress core so that is fine. It is also best to enqueue any scripts to prevent plugins loading scripts multiple times and allowing for dependencies. So should never be done in your header.php - Thumbs up for doing that!
    – Matt Royal
    Commented Jul 11, 2014 at 11:15
  • Oh, you are telling that I'm in wrong way? By adding wp_enqueue_script("jquery"); in my header.php will fix my problem. Is that OK?
    – Jithin
    Commented Jul 11, 2014 at 11:23
  • 1
    You should use the 'wp_enqueue_script()' hook in your themes functions.php file not in your header.php file :-)
    – Matt Royal
    Commented Jul 11, 2014 at 11:36
  • Can you give me wp_enqueue_script() , that I need to paste in my functions.php file . My functions.php file
    – Jithin
    Commented Jul 11, 2014 at 11:39

1 Answer 1

1

Here is the correct way to include jQuery in your theme.

Open functions.php in your theme folder and add this to it:

 // Proper way to enqueue scripts and styles
    function royal_insert_jquery(){
    wp_enqueue_script('jquery', false, array(), false, false);
    }
    add_filter('wp_enqueue_scripts','royal_insert_jquery',1);

This will load the WordPress bundled version of jQuery and will make sure that it is always loaded into your head of the site.

EDIT

If you want to enqueue your own or existing jQuery rather than the bundled version then you can modify the hook by passing it a $src parameter.

function royal_insert_jquery(){

    wp_deregister_script('jquery');
    wp_register_script('jquery', get_template_directory_uri().'/javascripts/jquery.js', array(), false, false);
    wp_enqueue_script('jquery');
}

add_filter('wp_enqueue_scripts', 'royal_insert_jquery', 1);

Use the WordPress Codex as a reference so you know what parameters are required and can be passed and how to go about using the hook correctly. Here is the link: wp_enqueue_scripts()

4
  • So you are telling I need not remove <script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts/jquery.js"></script> from header.php and simply paste function royal_insert_jquery(){ wp_enqueue_script('jquery', false, array(), false, false); } add_filter('wp_enqueue_scripts','royal_insert_jquery',1); in my functions.php . Is that OK?
    – Jithin
    Commented Jul 11, 2014 at 11:48
  • Correct, yes. Unless you have other scripts in your theme that require a specific version of jQuery and are not compatible with the bundled version then you would still use the wp_enqueue_script() hook, just slightly differently and you would reference the script source in your theme within the hook.
    – Matt Royal
    Commented Jul 11, 2014 at 11:53
  • I pasted above script in my functions.php nothing happened sir.problem not solved. – But when I remove <script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts/jquery.js"></script> from header.php everything solved. I don't know what is going on!!
    – Jithin
    Commented Jul 11, 2014 at 12:09
  • 1
    Is it not possible that jQuery is trying to be loaded twice? Check your source code and look for that. By removing it from your header.php you are solving the problem because it is then only loading one instance of jQuery and not two. In which case you don't need to enqueue jQuery because it is already happening elsewhere then.
    – Matt Royal
    Commented Jul 11, 2014 at 12:14

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.