Posted By

f6design on 11/29/11


Tagged

wordpress jquery CDN


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Kurt
letunglam
meninoebom


Load jQuery from CDN, fallback to local (for Wordpress)


 / Published in: PHP
 

Add the following to your Wordpress theme's functions.php file. The jQuery paths/URLs might need to be changed to suite your circumstances.

  1. /**
  2.  * Load jQuery from Google CDN, fallback to local
  3.  */
  4. // http://wp.tutsplus.com/tutorials/load-jquery-from-google-cdn-with-local-fallback-for-wordpress/
  5. if( !is_admin()){ // Don't do this for admin area, since Google's jQuery isn't in noConflict mode and will interfere with WP's admin area.
  6. $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'; // the URL to check against
  7. $test_url = @fopen($url,'r'); // test parameters
  8. if($test_url !== false) { // test if the URL exists
  9. function load_external_jQuery() { // load external file
  10. wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery
  11. wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'); // register the external file
  12. wp_enqueue_script('jquery'); // enqueue the external file
  13. }
  14. add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function
  15. } else {
  16. function load_local_jQuery() {
  17. wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
  18. wp_register_script('jquery', get_bloginfo('template_url').'/js/jquery-1.6.2.min.js', __FILE__, false, '1.6.2', true); // register the local file
  19. wp_enqueue_script('jquery'); // enqueue the local file
  20. }
  21. add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function
  22. }
  23. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: aaroncampbell on September 6, 2012

There are a few problems with this: * You shouldn't package your own jQuery, just use the one WP came with. To do this, inside the else just use wpenqueuescript('jquery') * An fopen on every front-end page load is pretty heavy. Maybe cache your results in a persistent cache and only check every 5 minutes? * Also, the fact that you can open a file from your server doesn't mean your user can open it from their computer. If there's something wrong with their route to Google apis but not the route between your server and Google apis, they'll still have issues.

Posted By: DreamsEngineering on July 3, 2013

Well , I'm not getting a good solution form that approach ... so I'm trying: 'if (!is_admin()) {

    $jQueryCDN = '//ajax.googleapis.com/ajax/libs/jquery/'.$jQueryVer.'/jquery.min.js';
    $jQueryLocal = get_bloginfo('template_url') . '/js/vendor/jquery.min.js'

//Deregister Scripts wpderegisterscript('jquery');

    /* Jquery */

    if (get_transient('google_jquery') == true) {
        wp_register_script('jquery', $jQueryCDN, array(), null, true);
    }
    else {
        $resp = wp_remote_head($jQueryCDN);
        if (!is_wp_error($resp) && 200 == $resp['response']['code']) {
            set_transient('google_jquery', true, 60 * 5);
            wp_register_script('jquery', $jQueryLocal, array(), null, true);
        }
        else {
            set_transient('google_jquery', false, 60 * 5);
            wp_register_script('jquery', $jQueryCDN, array(), ''.$jQueryVer.'', true);
        }
    }

//Enqueue Scripts wpenqueuescript('jquery');

}'

Posted By: DreamsEngineering on July 3, 2013

Sorry , snippler markup change everything... so check it here: https://gist.github.com/DreamsEngineering/5910227

You need to login to post a comment.