Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Last active May 11, 2017 20:20
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 macbookandrew/0bb0c29b2321c166845cea0b7cdb5d15 to your computer and use it in GitHub Desktop.
Save macbookandrew/0bb0c29b2321c166845cea0b7cdb5d15 to your computer and use it in GitHub Desktop.
Force merged scripts created by “Merge + Minify + Refresh” to load asynchronously to improve site performance
<?php
/*
* Plugin Name: Merge + Minify + Refresh: Force Async Scripts
* Plugin URL: https://gist.github.com/macbookandrew/0bb0c29b2321c166845cea0b7cdb5d15
* Description: Forces merged scripts created by “Merge + Minify + Refresh” to load asynchronously to improve site performance
* Version: 1.0.0
* Author: AndrewRMinion Design
* Author URI: https://andrewrminion.com
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Add async attribute if the path contains MMR’s cache directory
* @param string $tag HTML <script> tag
* @param string $handle WordPress’ internal handle for this script
* @return string HTML <script> tag
*/
function mmr_add_async( $tag, $handle ) {
if ( strpos( $tag, WP_CONTENT_URL . '/mmr' ) !== false ) {
$tag = str_replace( ' src', ' async="async" src', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'mmr_add_async', 99, 2 );
@macbookandrew
Copy link
Author

macbookandrew commented May 11, 2017

Changelog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment