Last active
December 26, 2024 16:08
-
-
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
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 | |
/* | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changelog