Created
July 24, 2019 02:04
-
-
Save csaborio001/1bf2889154146f2918510b1690e8962f to your computer and use it in GitHub Desktop.
Checks to see if a script declaration contains #asyncload or #deferload and inserts * the attribute in the script declaration if it is found. Based on this: https://tinyurl.com/y2obn9os
This file contains hidden or 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
/** | |
* add_attribute_for_script - Checks to see if a script declaration contains #asyncload or #deferload and inserts | |
* the attribute in the script declaration if it is found. Based on this: https://tinyurl.com/y2obn9os | |
* | |
* @param string $url - the URL to check to see if it contains #asyncload or #deferload | |
* | |
* @return string - the modified string if any change was required. | |
*/ | |
function add_attribute_for_script( $url ) | |
{ | |
if ( ( strpos( $url, '#asyncload')===false ) && ( strpos( $url, '#defer')===false ) ) { // No attribute specified. | |
return $url; | |
} | |
else if ( is_admin() ) { // We're in the WP Admin Interface. | |
return str_replace( '#asyncload', '', $url ); | |
} | |
else if ( strpos( $url, '#asyncload' )!==false ) { // asyncload was found. | |
return str_replace( '#asyncload', '', $url)."' async='async"; | |
} | |
else { // Has to be Defer. | |
return str_replace( '#deferload', '', $url)."' defer='defer"; | |
} | |
} | |
add_filter('clean_url', 'add_attribute_for_script', 11, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment