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.
1 Answer
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()
-
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 pastefunction 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?– JithinCommented 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. 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!!– JithinCommented Jul 11, 2014 at 12:09 -
1Is 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. Commented Jul 11, 2014 at 12:14
wp_enqueue_script("jquery");
in my header.php will fix my problem. Is that OK?