Instantly share code, notes, and snippets.

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

This comment has been minimized.

Owner

macbookandrew commented May 11, 2017

Changelog

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