Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Defer parsing Javascript Firoze
- Plugin URI:
- Description: Just a defer parsing javascript plugin for WordPress site.
- Author: Firoze
- Version: 1.0.0
- Author URI:
- */
- /*-----------------------------------------------------
- *Latest Jquery For WP defer parsing javascript
- *------------------------------------------------------*/
- function defer_parsing_javascript() {
- wp_enqueue_script( 'jquery' );
- }
- add_action( 'init', 'defer_parsing_javascript' );
- /*-----------------------------------------------------
- * Some predefine Set-up for WP defer parsing javascript
- *-----------------------------------------------------*/
- define('WP_defer_PlUGIN', WP_PLUGIN_URL . '/' . plugin_basename( dirname(__FILE__) ) . '/' );
- /*------------------------------------------------------------
- * Main Jquery and Style for WP defer parsing javascript
- *------------------------------------------------------------*/
- function wp_defer_main_jquery() {
- wp_enqueue_script( 'wp-defer-parsing', WP_defer_PlUGIN.'js/defer.js', array('jquery'), 1.0, false);
- }
- add_action( 'init', 'wp_defer_main_jquery' );
- // Defer Javascripts
- // Defer jQuery Parsing using the HTML5 defer property
- if (!(is_admin() )) {
- function defer_parsing_of_js ( $url ) {
- if ( FALSE === strpos( $url, '.js' ) ) return $url;
- if ( strpos( $url, 'jquery.js' ) ) return $url;
- // return "$url' defer ";
- return "$url' defer onload='";
- }
- add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
- }
- // Move all JS from header to footer
- if(!is_admin()) {
- remove_action('wp_head', 'wp_print_scripts');
- remove_action('wp_head', 'wp_print_head_scripts', 9);
- remove_action('wp_head', 'wp_enqueue_scripts', 1);
- add_action('wp_footer', 'wp_print_scripts', 5);
- add_action('wp_footer', 'wp_enqueue_scripts', 5);
- add_action('wp_footer', 'wp_print_head_scripts', 5);
- }
- // Defer JS for Contact Form 7
- ! defined( 'ABSPATH' ) and exit;
- if ( ! function_exists( 'add_defer_to_cf7' ) )
- {
- function add_defer_to_cf7( $url )
- {
- if ( // comment the following line out add 'defer' to all scripts
- // FALSE === strpos( $url, 'contact-form-7' ) or
- FALSE === strpos( $url, '.js' )
- )
- { // not our file
- return $url;
- }
- // Must be a ', not "!
- return "$url' defer='defer";
- }
- add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );
- }
- // Remove query string from static files
- function ewp_remove_scriptP( $src ){
- return remove_query_arg( 'ver', $src );
- }
- add_filter( 'script_loader_src', 'ewp_remove_scriptP', 15, 1 );
- add_filter( 'style_loader_src', 'ewp_remove_scriptP', 15, 1 );
- /*---------------------------------------------------
- * Add script in footer for WP defer parsing javascript
- *---------------------------------------------------*/
- function wp_def_active_foot(){
- ?>
- <?php
- }
- add_action('wp_footer', 'wp_def_active_foot');
- /*---------------------------------------------------
- * Add script in header for WP defer parsing javascript
- *---------------------------------------------------*/
- function wp_def_active_hed(){
- ?>
- <script type="text/javascript">
- function downloadJSAtOnload() {
- var element = document.createElement("script");
- element.src = "www.ideal-pro.com/Avada/wp-content/plugins/deferparsingjavascript/js/defer.js"; // defer.js
- //element.src = "http://localhost/homelandwp/wp-content/plugins/deferparsingjavascript/js/defer.js"; // defer.js
- document.body.appendChild(element);
- }
- if (window.addEventListener)
- window.addEventListener("load", downloadJSAtOnload, false);
- else if (window.attachEvent)
- window.attachEvent("onload", downloadJSAtOnload);
- else window.onload = downloadJSAtOnload;
- </script>
- <?php
- }
- add_action('wp_head', 'wp_def_active_hed');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement