Guest User

Untitled

a guest
Jan 11th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. wp_enqueue_script( 'bootstrap', 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js', array('jquery'), 3.3, true);
  2.  
  3. $get_the_url = 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js';
  4.  
  5. $cdnIsUp = get_transient( 'cnd_is_up' );
  6.  
  7. if ( $cdnIsUp ) {
  8.  
  9. $load_source = 'load_external_bootstrap';
  10.  
  11. } else {
  12.  
  13. $cdn_response = wp_remote_get( $get_the_url );
  14.  
  15. if( is_wp_error( $cdn_response ) || wp_remote_retrieve_response_code($cdn_response) != '200' ) {
  16.  
  17. $load_source = 'load_local_bootstrap';
  18.  
  19. }
  20. else {
  21.  
  22. $cdnIsUp = set_transient( 'cnd_is_up', true, MINUTE_IN_SECONDS * 20 );
  23. $load_source = 'load_external_bootstrap';
  24. }
  25. }
  26.  
  27. add_action('wp_enqueue_scripts', $load_source );
  28.  
  29. function load_external_bootstrap() {
  30. wp_register_script( 'bootstrap', 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js', array('jquery'), 3.3, true);
  31. wp_enqueue_script('bootstrap');
  32. }
  33.  
  34. function load_local_bootstrap() {
  35. wp_register_script('bootstrap', get_bloginfo('template_url').'/js/bootstrap.min.js', __FILE__, array('jquery'), 3.3, true);
  36. wp_enqueue_script('bootstrap');
  37. }
  38.  
  39. requirejs.config({
  40. enforceDefine: true,
  41. paths: {
  42. jquery: [
  43. '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min',
  44. //If the CDN location fails, load from this location
  45. //xyz.com/jquery.min.js
  46. ]
  47. }
  48. });
  49.  
  50. $get_the_url = 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js';
  51. $test_the_url = @fopen( $get_the_url,'r' );
  52.  
  53. if ( $test_the_url !== false ) {
  54.  
  55. function load_external_bootstrap() {
  56. wp_register_script( 'bootstrap', 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js', array('jquery'), 3.3, true);
  57. wp_enqueue_script('bootstrap');
  58.  
  59. }
  60.  
  61. add_action('wp_enqueue_scripts', 'load_external_bootstrap');
  62.  
  63. } else {
  64.  
  65. function load_local_bootstrap() {
  66. wp_register_script('bootstrap', get_bloginfo('template_url').'/js/bootstrap.min.js', __FILE__, array('jquery'), 3.3, true);
  67. wp_enqueue_script('bootstrap');
  68. }
  69.  
  70. add_action('wp_enqueue_scripts', 'load_local_bootstrap');
  71. }
  72.  
  73. function wp_enqueue_cdn_script( $handle, $src_cdn = false, $src_local = false, $deps = array(), $ver = false, $in_footer = false ) {
  74. $cdnIsUp = get_transient( $handle . '_script_cdn_is_up' );
  75. if ( $cdnIsUp ) {
  76. wp_enqueue_script( $handle, $src_cdn, $deps, $ver, $in_footer );
  77. } else {
  78. $cdn_response = wp_remote_get( $src_cdn );
  79. if ( is_wp_error( $cdn_response ) || wp_remote_retrieve_response_code( $cdn_response ) != '200' ) {
  80. wp_enqueue_script( $handle, $src_local, $deps, $ver, $in_footer );
  81. } else {
  82. $cdnIsUp = set_transient( $handle . '_script_cdn_is_up', true, MINUTE_IN_SECONDS * 20 );
  83. wp_enqueue_script( $handle, $src_cdn, $deps, $ver, $in_footer );
  84. }
  85. }
  86. }
  87.  
  88. function wp_enqueue_cdn_style( $handle, $src_cdn = false, $src_local = false, $deps = array(), $ver = false, $media = 'all' ) {
  89. $cdnIsUp = get_transient( $handle . '_style_cdn_is_up' );
  90. if ( $cdnIsUp ) {
  91. wp_enqueue_style( $handle, $src_cdn, $deps, $ver, $media);
  92. } else {
  93. $cdn_response = wp_remote_get( $src_cdn );
  94. if ( is_wp_error( $cdn_response ) || wp_remote_retrieve_response_code( $cdn_response ) != '200' ) {
  95. wp_enqueue_style( $handle, $src_local, $deps, $ver, $media);
  96. } else {
  97. $cdnIsUp = set_transient( $handle . '_style_cdn_is_up', true, MINUTE_IN_SECONDS * 20 );
  98. wp_enqueue_style( $handle, $src_cdn, $deps, $ver, $media);
  99. }
  100. }
  101. }
  102.  
  103. function theme_styles()
  104. {
  105. wp_enqueue_style( 'bootstrap_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
  106.  
  107. }
  108. add_action( 'wp_enqueue_scripts', 'theme_styles');
  109.  
  110. global $wp_scripts;
  111.  
  112.  
  113. wp_enqueue_script( 'bootstrap_js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js');
  114.  
  115.  
  116. }
  117. add_action( 'wp_enqueue_scripts', 'theme_js');
Add Comment
Please, Sign In to add comment