Created
April 4, 2014 05:22
-
-
Save AbhishekGhosh/9968635 to your computer and use it in GitHub Desktop.
Defer-Javascript WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare( encoding = 'UTF-8' ); | |
// Optional Junk | |
/** | |
* Plugin Name: Defer Scripts | |
* Description: Add defer to enqueued javascripts | |
* Version: 1.0 | |
* Required: 3.3 | |
* Author URI: http://www.gnu.org/ | |
* License: GNU GPL v3.0 | |
* License URI: http://www.gnu.org/copyleft/gpl.html | |
* | |
*/ | |
// You can modify and use your own name above | |
! defined( 'ABSPATH' ) and exit; | |
// Change YOURID to small letter specific name | |
if ( ! function_exists( 'add_defer_to_YOURID' ) ) | |
{ | |
function add_defer_to_YOURID( $url ) | |
{ | |
if ( | |
// We are deferring a specific script, the plugin is plugin-name | |
FALSE === strpos( $url, 'plugin-name' ) or | |
FALSE === strpos( $url, '.js' ) | |
) | |
{ | |
return $url; | |
} | |
// Get the things done | |
return "$url' defer='defer"; | |
} | |
// The number should not conflict with any eneque function | |
add_filter( 'clean_url', 'add_defer_to_YOURID', 15, 7 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read here : http://thecustomizewindows.com/2014/04/defer-parsing-javascript-wordpress-snippets-plugins/
And here : http://thecustomizewindows.com/2013/11/eliminate-render-blocking-javascript-and-css-wordpress/
For more details.