Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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