Browse > Home / development, plugins / Google AJAX Libraries API Plugin

| Subcribe via RSS

Google AJAX Libraries API Plugin

May 28th, 2008 Posted in development, plugins

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Update: I have updated this plugin to version 0.9.

Earlier this week Google announced their Ajax Libraries API.

The AJAX Libraries API takes the pain out of developing mashups in JavaScript while using a collection of libraries. We take the pain out of hosting the libraries, correctly setting cache headers, staying up to date with the most recent bug fixes, etc.

The Google Ajax Libraries API Plugin (what a mouthfull) is designed to make it easy to use the Google hosted libraries without the need to mess with your theme and plugin code.

When activated, the plugin will “listen” to all of the scripts added (via the WordPress wp_enqueue_script function) to your pages header and automagically switch the locally hosted library to a Google hosted one. Deactivating the plugin will remove the “listener” and your site will return to using the local versions.

WordPress MU administrators should copy the plugin into their MU-Plugins directory for it to be enabled across all of your hosted blogs.

The latest version of this plugin is available for download below.

downloadzip
Changelog
0.9 -4th September 2008
This version now uses non-version specific Google URL’s (/1/ instead of /1.6.1/, etc) to ensure that the latest hosted version of the Javascript library is loaded.
Support has also been added for the jQuery UI library (note: you need to include the jQuery library if you want to use the jQuery UI library, this plugin does not do that for you).

25 Responses to “Google AJAX Libraries API Plugin”

  1. TaopaiC Says:

    Hello, it is a great plugin!! But it seems a problem with jquery and prototype conflicting.

    I wrote a patch for it, please refer my blog (sorry , it was written in chinese).


  2. Barry Says:

    @TaopaiC: Thanks for the message. I haven’t had a problem with jquery and prototype on the sites I’m using it on as yet - but I will have a look at your solution with a view to integrating it.


  3. xiaomo Says:

    so quick~~
    thanks~~


  4. Steve Says:

    B,

    This is a great idea.

    When I got the annoucnement about Google’s API two days ago I manually commented out my existing jQuery in favor of Google’s “new way”.

    Can I ask you… let’s say I uncommented MY local jquery reference… AND have your plugin working. How does your plugin tell WordPress (or my browser) not to pay attention to my local jQuery reference? Do they have to be named exactly the same? (because I always name my local latest version to “jquery-latest.js” so that I don’t have to change my theme template whenever I want to throw up the latest jQuery version).


  5. Barry Says:

    @Steve: The plugin hooks into the interface used by wp_enqueue_script, so with jQuery for example, if any themes or plugins enqueue jQuery, the library (jquery.js) is replaced when output by the Google library (jquery-min.js). Any libraries that are hardcoded into themes or plugins are ignored.


  6. Steve Says:

    Oh, so if previously my “header.php” file was adding jQuery with a simple [script src='path/jquery.js'][/script] that wouldn’t matter one way or the other?

    Perhaps I’m enqueue ignorant. :-) I’ll have to look it up.


  7. johnbillion Says:

    Hi Barry,

    Coincidentally I also wrote and released a Google AJAX Libraries API plugin for WordPress but I’ve chosen to use the wp_print_scripts action hook instead of filtering the script_loader_src . Check out my Google AJAX Libraries API WordPress plugin.


  8. Barry Says:

    @TaopaiC: I’ve updated the plugin to handle the loading of jQuery and Prototype on the same page. I’ve incorporated part of your solution, but have removed the need for an extra javascript file containing the noconflict code. Have a look and let me know what you think.


  9. TaopaiC Says:

    It works good.

    thanks Barry :)


  10. James D Kirk Says:

    Getting this little bummer (x4) at the top of my admin pages after activating the plugin:


    Warning: preg_match() [function.preg-match]: Compilation failed: unrecognized character after (?< at offset 3 in /var/www/vhosts/thefightchannel.com/httpdocs/wp-content/plugins/usegooglelibrary.php on line 26

    Thoughts?


  11. david Says:

    effects.js can be loaded from Google too…

    “/effects.js” => “http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/effects.js”


  12. Barry Says:

    @James D Kirk: What version of php are you using? The latest version of the plugin uses the named subpattern functionality of preg_match which may not be in early versions. if you can confirm the version you are using - then I’ll write a short workaround for it.


  13. Jordan Says:

    I’m getting the same error as James D Kirk, and I have PHP 5.


  14. Barry Says:

    @Jordan & James D Kirk: I have updated the plugin to version 0.7 and removed the named subpattern from the regular expression check. Fingers crossed this should get rid of the problem you have been having.


  15. James D Kirk Says:

    Hey Barry. Apologies, yes, like Jordan I am running php5. And the 0.7 release does seem to make those errors go away.
    New question, however, is how do we “know” your plugin is actually working? What can I check or is there code I can view to see that in fact I am pulling code from the libraries? Curious. Thanks!


  16. Barry Says:

    @James D Kirk: The easiest way is if you have Firebug installed with the Firefox browser. Have the Net tab visible when you load your page and it will show which locations it loads each part of your page from. Alternatively just look at the source of your page and check for javascript includes from Google for the jQuery and other libraries


  17. Thad Says:

    Can this plugin also be used in theme files to pull in the requested javascript library if requested? The way I understand this plugin is that it adds the latest js library to your Wordpress install, not your theme files. Is this correct? If so, is there a call that I can make in my header to pull in the latest scripts into the head of my site?


  18. Barry Says:

    @Thad: In your theme, rather than including the loading of the javascript library in your theme header.php file, add something along the lines of the following to your themes functions.php file:

    if(function_exists(’wp_enqueue_script’)) {
    wp_enqueue_script(’jquery’);
    }

    This will then load the jQuery library for your theme in a manner that will make changeable to the google libraries by the plugin.


  19. Barry Says:

    The google ajax plugin is now at version 0.8. If you are using version 0.7 and DO NOT need to load the Scriptaculous libraries (e.g. effects.js, dragdrop.js) separately then you don’t need to upgrade.


  20. RT Cunningham Says:

    I had a problem running Ajax Edit Comments and found out it was the 1.2.6/jquery.min.js. Since WordPress 2.5.1 uses 1.2.3/jquery.js, I changed that line and then AEC worked again. I don’t know why it should matter but it does.


  21. Barry Says:

    @RT Cunningham: Thanks for the info. I’ve not had a problem with the versions yet, everything seems to function as before. Out of interest, do you know what error the Ajax Edit Comments plugin was throwing up?


  22. RT Cunningham Says:

    No errors. The comments wouldn’t load and the buttons/links in the edit panel wouldn’t work.


  23. Barry Says:

    @david: Version 0.8 now adds checks for the indiviual scriptaculous libraries.


  24. Paul Paradise Says:

    Hi Barry,

    There’s a minor bug in the jquery-conflict code in v0.8 - replace the beginning of cs_handlejqueryconflict() with this:

    $jquerypos = array_search(’jquery’, $args);

    if(false !== $jquerypos && in_array(’prototype’, $args)) {

    In the other code, the && operator binds too tightly, resulting in the jquerypos variable not actually be the position of the array.


  25. vasco Says:

    Someone needs to post this over at http://www.ajaxtutorials.com.


Leave a Reply

Tags:
Separate individual tags by commas