Last active
November 8, 2021 23:58
-
-
Save otakupahp/b4025ef167eb41ae7f9fafbb0dc66362 to your computer and use it in GitHub Desktop.
Adds support for 'integrity' and 'crossorigin' at enqueuing a script on WordPress
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 | |
/** | |
* Adds support for 'integrity' and 'crossorigin' | |
* | |
* This relies on the function *wp_script_add_data* been called when enqueuing the script | |
* | |
* @param $tag | |
* @param $handle | |
* | |
* @return string | |
*/ | |
function filter_script_loader_tag( $tag, $handle ) { | |
foreach ( array('integrity','crossorigin') as $attr ) { | |
if ( $value = wp_scripts()->get_data( $handle, $attr ) ) { | |
$extra = sprintf( '<script %s="%s"', $attr, $value ); | |
$tag = str_replace('<script', $extra, $tag); | |
} | |
} | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: