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

JQuery Conflict - Plugins messing things up WordPress

  • SOLVED

Hi There,

I'm working on my custom wordpress theme. I'm new to theme development but have learned tons thanks to everyone on this site.

I am running into an issue with jQuery. When my customers install their theme and then install a whole bunch of plugins, my default jQuery file being called in no longer gets called in. This is causing certain functionalities not to work properly.

Is there anything I can do to my jQuery file to make sure that no matter what plugins they install, it will not affect my jQuery funtionality?

Here is the code I am using to pull in the JQuery. I thought this prvents other instances of jQuery from loading in but I guess not.

Any help is greatly appreciated.


<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.2.6.min.js"></script>

Answers (2)

2010-04-23

Merne Asplund answers:

I ran into a similar issue. This seemed to work for me:



<?php
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');
?>



This should go in your themes function file.


WP Answers comments:

Hi,

Thank you for this. Which part of that code do I use? Should I get rid of what I currently have in my header.php?


WP Answers comments:

Thank you for this, but it doesn't seem to be doing anything.


Here is the situation. I have the exact theme theme running on 2 different servers. Everything works perfectly on one server because it does not have any conflicting plug-ins. On the other server they have a bunch of plug-ins and one or more of them are obviously conflicting.

When I view the source code, the verison of the site with the issues does not call in my javascript file, but instead, a few lines down another version of jQuery is being called.

Isn;t there something I can do, just as this plugin has done, that removes any other instance of jquery and inputs mine?

If not I compeltely understand. I'll just have to let my client know they have to lose their plugins.


Merne Asplund comments:

On second thought, you may just want to add 'wp_deregister_script( 'jquery' );' in place of your 'wp_enqueue_script("jquery");'. It seems you are adding jquery more than once here in two different ways. Also, we will do this after wp_head() to make sure the jquery loaded is your version.

so to recap you should have:



<?php wp_head(); ?>

<?php wp_deregister_script("jquery"); ?>

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.2.6.min.js"></script>


WP Answers comments:

Thank you, but still no dice. There is no change at all. The code still renders just as before.


Merne Asplund comments:

I must warn you that this may not work if a plugin is loading their jquery somewhere outside of the init or admin_head events.


WP Answers comments:

This worked!

Sorry, but the client switched the theme in his back-end while I was making changes so I was making changes to the wrong theme.

This works perfectly:

<?php wp_head(); ?>
<?php wp_deregister_script("jquery"); ?>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/jquery-1.2.6.min.js"></script>



Thank you very much - I really appreciate it.


Merne Asplund comments:

Are you using plugins that are compatible with the current version of wordpress you are running?


Merne Asplund comments:

nvm. your very welcome.

2010-04-23

Buzu B answers:

you can add the call to your jquery directly in the header.php file. This file is always called so, it is pretty safe to include it there.

You can also include it in footer.php at the very end...


WP Answers comments:

Hi Buzu,

This is where it currently gets called. But when users install plugins it seems to take mine out and add in a different jquery file? I notice this because I compare the source code of the theme on their site, and the code on my site


Buzu B comments:

I see...

If the plugin is using another version of jquery, you might not be able to make it work. Even if you could add both files, one will always overwrite the other. The best solution would be to port your code so it works with the version of jquery the plugin uses, though I wouldn't recommend it if the version of jquery the plugin uses is older than the one you use.


WP Answers comments:

Hmm..Ok.

I'm surprised there is no way around this conflict.


Buzu B comments:

The thing is that at the end of every jQuery version, jquery does this:

<blockquote>window.jQuery = window.$ = jQuery;
</blockquote>

So, even if you add both files, when jquery gets exposed to the global environment, it gets exposed on the same variable, so when the second jquery file gets loaded it will overwrite the first one. I have never actually tried this but I'm sure that is what will happen.