Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
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 Jul 11 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? –  Vijitha Stephy Jul 11 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 Jul 11 at 11:36
    
Can you give me wp_enqueue_script() , that I need to paste in my functions.php file . My functions.php file –  Vijitha Stephy Jul 11 at 11:39

1 Answer 1

up vote 1 down vote accepted

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()

share|improve this answer
    
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? –  Vijitha Stephy Jul 11 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 Jul 11 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!! –  Vijitha Stephy Jul 11 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 Jul 11 at 12:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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