Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created April 4, 2014 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/9968635 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/9968635 to your computer and use it in GitHub Desktop.
Defer-Javascript WordPress
<?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