Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Prevent Wordpress from adding jQuery? WordPress

  • SOLVED

Hi There,

I am building a custom wordpress site for a client of mine. For whatever reason wordpress is automatically loading the default jQuery script into header.php. I need to load my own jquery script rather than the default script.

Here is the code I am using to call in my javascript. This is a separate PHP file which I have included into my functions.php file. All of the scripts get loaded in perfectly besides for the first one, "jquery.min.js". Instead wordpress is loading its own jquery.

How can I prevent wordpress from adding the default jQuery? I would like to load all of my javascript files just as I've listed them below.

I really appreciate your help.




<?php
function THEME_scripts() {
wp_enqueue_script( 'jquery', THEME_JS .'/jquery.min.js', array('jquery'));
wp_enqueue_script( 'jquery-accordion', THEME_JS .'/jquery.cycle.all.2.74.js', array('jquery'));
wp_enqueue_script( 'cufon', THEME_JS .'/cufon.js', array('jquery'));
wp_enqueue_script( 'cufon-fonts', THEME_JS .'/cufon-fonts.js', array('jquery'));
wp_enqueue_script( 'cufon-settings', THEME_JS .'/cufon-settings.js', array('jquery'));
wp_enqueue_script( 'THEME-custom', THEME_JS .'/home_modules.js', array('jquery'));
}

add_action('wp_print_scripts', 'THEME_scripts');
?>

Answers (4)

2010-08-29

flashingcursor answers:

WordPress already has jquery registered as a script, so if you want a different jquery to load, you need to deregister it first:

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

Replace the googleapis location with whichever you want to use.


WP Answers comments:

Ok Thank you. Would I add this to my custom PHP file that I listed above?


flashingcursor comments:

Yes

function my_init_method() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');
}

add_action('init', 'my_init_method');

That code should do exactly what you need. Notice that we add_action on init to deregister / register a new script, so you can't just add it to your function.


flashingcursor comments:

Also, once you deregister and register your version of the script, all you have to do to call it is

wp_enqueue_script( 'jquery');

The additional options are unnecessary.


WP Answers comments:

Thank you very much. Everything is now getting loaded in fine.

However for some reason my jquery animation is not working. I am using the jquery cycle plugin to rotate a banner area. I have the static HTML version that is identical to the wordpress version but the wordpress version is not working.

Could the version at the end of the scripts be causing any kind of issue? Is there anyway to prevent those version numbers from being printed?

Here's an example. Here is what the code looks like when the page has loaded in wordpress:

<script type='text/javascript' src='http://localhost:8888/www.site.com/wp-content/themes/custom-theme/js/jquery.min.js?ver=3.0.1'></script>
<script type='text/javascript' src='http://localhost:8888/www.site.com/wp-content/themes/custom-theme/js/jquery.cycle.all.2.74.js?ver=3.0.1'></script>

<script type='text/javascript' src='http://localhost:8888/www.site.com/wp-content/themes/custom-theme/js/home_modules.js?ver=3.0.1'></script>




...and here is what it looks like in the static HTML version (which is functioning properly)


<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.cycle.all.2.74.js"></script>
<script type="text/javascript" src="js/home_modules.js"></script>



Could the ?ver=3.0.1' be causing an error? Can that be turned off?

2010-08-29

West Coast Design Co. answers:

Hey Five Squared,

Turn off each plug-in one by one … and check your source, swear its probably a plug-in, I had a similar issue with Gravity Forms; [[LINK href="http://wcdco.info/a6"]]http://wcdco.info/a6[[/LINK]]

Cheers,


WP Answers comments:

Thanks for the info but I don't have any plugins at all installed.

2010-08-29

Mike Truese answers:

flashingcursor has the right answer.

2010-08-29

Bingu Zhong answers:

Add init function to your plugin:


function init_THEME_scripts() {
wp_deregister_script( 'jquery' );

wp_register_script( 'jquery', THEME_JS .'/jquery.min.js');

}
add_action('init', 'my_init_method');

function THEME_scripts() {

wp_enqueue_script( 'jquery', THEME_JS .'/jquery.min.js', array('jquery'));

wp_enqueue_script( 'jquery-accordion', THEME_JS .'/jquery.cycle.all.2.74.js', array('jquery'));

wp_enqueue_script( 'cufon', THEME_JS .'/cufon.js', array('jquery'));

wp_enqueue_script( 'cufon-fonts', THEME_JS .'/cufon-fonts.js', array('jquery'));

wp_enqueue_script( 'cufon-settings', THEME_JS .'/cufon-settings.js', array('jquery'));

wp_enqueue_script( 'THEME-custom', THEME_JS .'/home_modules.js', array('jquery'));

}



add_action('wp_print_scripts', 'THEME_scripts');


you can check the [[LINK href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script"]]function reference[[/LINK]] for more infomation.