Advertisement
firoze

Defer javascript for for page speed

Jan 11th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Defer parsing Javascript Firoze
  5. Plugin URI:
  6. Description: Just a defer parsing javascript plugin for WordPress site.
  7. Author: Firoze
  8. Version: 1.0.0
  9. Author URI:
  10. */
  11.  
  12.  
  13. /*-----------------------------------------------------
  14. *Latest Jquery For WP defer parsing javascript
  15. *------------------------------------------------------*/
  16. function defer_parsing_javascript() {
  17. wp_enqueue_script( 'jquery' );
  18. }
  19. add_action( 'init', 'defer_parsing_javascript' );
  20.  
  21.  
  22.  
  23. /*-----------------------------------------------------
  24. * Some predefine Set-up for WP defer parsing javascript
  25. *-----------------------------------------------------*/
  26. define('WP_defer_PlUGIN', WP_PLUGIN_URL . '/' . plugin_basename( dirname(__FILE__) ) . '/' );
  27.  
  28.  
  29.  
  30. /*------------------------------------------------------------
  31. * Main Jquery and Style for WP defer parsing javascript
  32. *------------------------------------------------------------*/
  33. function wp_defer_main_jquery() {
  34.  
  35. wp_enqueue_script( 'wp-defer-parsing', WP_defer_PlUGIN.'js/defer.js', array('jquery'), 1.0, false);
  36.  
  37. }
  38. add_action( 'init', 'wp_defer_main_jquery' );
  39.  
  40.  
  41.  
  42.  
  43. // Defer Javascripts
  44. // Defer jQuery Parsing using the HTML5 defer property
  45. if (!(is_admin() )) {
  46. function defer_parsing_of_js ( $url ) {
  47. if ( FALSE === strpos( $url, '.js' ) ) return $url;
  48. if ( strpos( $url, 'jquery.js' ) ) return $url;
  49. // return "$url' defer ";
  50. return "$url' defer onload='";
  51. }
  52. add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
  53. }
  54.  
  55.  
  56. // Move all JS from header to footer
  57. if(!is_admin()) {
  58. remove_action('wp_head', 'wp_print_scripts');
  59. remove_action('wp_head', 'wp_print_head_scripts', 9);
  60. remove_action('wp_head', 'wp_enqueue_scripts', 1);
  61. add_action('wp_footer', 'wp_print_scripts', 5);
  62. add_action('wp_footer', 'wp_enqueue_scripts', 5);
  63. add_action('wp_footer', 'wp_print_head_scripts', 5);
  64. }
  65.  
  66. // Defer JS for Contact Form 7
  67.  
  68. ! defined( 'ABSPATH' ) and exit;
  69.  
  70. if ( ! function_exists( 'add_defer_to_cf7' ) )
  71. {
  72. function add_defer_to_cf7( $url )
  73. {
  74. if ( // comment the following line out add 'defer' to all scripts
  75. // FALSE === strpos( $url, 'contact-form-7' ) or
  76. FALSE === strpos( $url, '.js' )
  77. )
  78. { // not our file
  79. return $url;
  80. }
  81. // Must be a ', not "!
  82. return "$url' defer='defer";
  83. }
  84. add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );
  85. }
  86.  
  87.  
  88. // Remove query string from static files
  89. function ewp_remove_scriptP( $src ){
  90. return remove_query_arg( 'ver', $src );
  91. }
  92. add_filter( 'script_loader_src', 'ewp_remove_scriptP', 15, 1 );
  93. add_filter( 'style_loader_src', 'ewp_remove_scriptP', 15, 1 );
  94.  
  95.  
  96.  
  97. /*---------------------------------------------------
  98. * Add script in footer for WP defer parsing javascript
  99. *---------------------------------------------------*/
  100. function wp_def_active_foot(){
  101. ?>
  102.  
  103. <?php
  104. }
  105. add_action('wp_footer', 'wp_def_active_foot');
  106.  
  107.  
  108. /*---------------------------------------------------
  109. * Add script in header for WP defer parsing javascript
  110. *---------------------------------------------------*/
  111. function wp_def_active_hed(){
  112. ?>
  113. <script type="text/javascript">
  114. function downloadJSAtOnload() {
  115. var element = document.createElement("script");
  116.  
  117. element.src = "www.ideal-pro.com/Avada/wp-content/plugins/deferparsingjavascript/js/defer.js"; // defer.js
  118.  
  119. //element.src = "http://localhost/homelandwp/wp-content/plugins/deferparsingjavascript/js/defer.js"; // defer.js
  120.  
  121. document.body.appendChild(element);
  122. }
  123. if (window.addEventListener)
  124. window.addEventListener("load", downloadJSAtOnload, false);
  125. else if (window.attachEvent)
  126. window.attachEvent("onload", downloadJSAtOnload);
  127. else window.onload = downloadJSAtOnload;
  128. </script>
  129. <?php
  130. }
  131. add_action('wp_head', 'wp_def_active_hed');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement