labcoatmedia

Wordpress Google jQuery with fallback

May 31st, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. add_action( 'wp_enqueue_scripts', 'wps_enqueue_jquery' );
  3. /**
  4.  * Enqueue jQuery from Google CDN with fallback to local WordPress
  5.  *
  6.  * @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script
  7.  * @link http://codex.wordpress.org/Function_Reference/wp_register_script
  8.  * @link http://codex.wordpress.org/Function_Reference/wp_deregister_script
  9.  * @link http://codex.wordpress.org/Function_Reference/get_bloginfo
  10.  * @link http://codex.wordpress.org/Function_Reference/is_wp_error
  11.  * @link http://codex.wordpress.org/Function_Reference/set_transient
  12.  * @link http://codex.wordpress.org/Function_Reference/get_transient
  13.  *
  14.  * @uses get_transient()        Get the value of a transient.
  15.  * @uses set_transient()        Set/update the value of a transient.
  16.  * @uses is_wp_error()          Check whether the passed variable is a WordPress Error.
  17.  * @uses get_bloginfo()         returns information about your site.
  18.  * @uses wp_deregister_script() Deregisters javascripts for use with wp_enqueue_script() later.
  19.  * @uses wp_register_script()   Registers javascripts for use with wp_enqueue_script() later.
  20.  * @uses wp_enqueue_script()    Enqueues javascript.
  21.  */
  22. function wps_enqueue_jquery() {
  23.     // Setup Google URI, default
  24.     $protocol = ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ? 'https' : 'http';
  25.     // Get Latest Version
  26.     $url      = $protocol . '://code.jquery.com/jquery-latest.min.js';
  27.    
  28.     // Get Specific Version
  29.     //$url      = $protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
  30.    
  31.     // Setup WordPress URI
  32.     $wpurl = get_bloginfo( 'wpurl') . '/wp-includes/js/jquery/jquery.js';
  33.    
  34.     // Setup version
  35.     $ver = null;
  36.    
  37.     // Deregister WordPress default jQuery
  38.     wp_deregister_script( 'jquery' );
  39.    
  40.     // Check transient, if false, set URI to WordPress URI
  41.     delete_transient( 'google_jquery' );
  42.    
  43.     if ( 'false' == ( $google = get_transient( 'google_jquery' ) ) ) {
  44.         $url = $wpurl;
  45.     }
  46.     // Transient failed
  47.     elseif ( false === $google ) {
  48.         // Ping Google
  49.         $resp = wp_remote_head( $url );
  50.        
  51.         // Use Google jQuery
  52.         if ( ! is_wp_error( $resp ) && 200 == $resp['response']['code'] ) {
  53.             // Set transient for 5 minutes
  54.             set_transient( 'google_jquery', 'true', 60 * 5 );
  55.         }
  56.        
  57.         // Use WordPress jQuery
  58.         else {
  59.             // Set transient for 5 minutes
  60.             set_transient( 'google_jquery', 'false', 60 * 5 );
  61.            
  62.             // Use WordPress URI
  63.             $url = $wpurl;
  64.            
  65.             // Set jQuery Version, WP stanards
  66.             $ver = '1.8.2';
  67.         }
  68.     }
  69.    
  70.     // Register surefire jQuery
  71.     wp_register_script( 'jquery', $url, array(), $ver, true );
  72.    
  73.     // Enqueue jQuery
  74.     wp_enqueue_script( 'jquery' );
  75. }
  76. ?>
Add Comment
Please, Sign In to add comment