Last active
January 24, 2020 04:51
-
-
Save lunaluna/5e6625be94301abcb0b1499d5efafc30 to your computer and use it in GitHub Desktop.
【WordPress】外部から読み込んでいる各スクリプト要素にハッシュ値と crossorigin="anonymous" を挿入する
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 | |
// 例えばそれぞれのスクリプトを CDN から読み込ませてるときは enqueue したあとにこんなふうに書くと追記できる | |
add_filter( 'script_loader_tag', 'add_attribs_to_scripts', 10, 3 ); | |
function add_attribs_to_scripts( $tag, $handle, $src ) { | |
$easing = array( 'easing' ); // ここは enqueue したときのハンドル名 | |
$popper = array( 'popper' ); | |
$bootstrap = array( 'bootstrap' ); | |
$validate = array( 'validate' ); | |
if ( in_array( $handle, $easing ) ) { | |
return '<script src="' . $src . '" integrity="sha384-gBfNhTkielNwzVn0UufwbbckhFJBgy58egVQZrzIknk9MmX5SmgCvdzrGsKlCW1P" crossorigin="anonymous"></script>' . "\n"; | |
} | |
if ( in_array( $handle, $popper ) ) { | |
return '<script src="' . $src . '" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>' . "\n"; | |
} | |
if ( in_array( $handle, $bootstrap ) ) { | |
return '<script src="' . $src . '" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>' . "\n"; | |
} | |
if ( in_array( $handle, $validate ) ) { | |
return '<script src="' . $src . '" integrity="sha384-38PYlsq0vWFYdiwAXpB0nFeTgsOMC8AyIFC5g/qDp0ihLbjeYiFAWz3rcNVR1+lI" crossorigin="anonymous"></script>' . "\n"; | |
} | |
return $tag; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ハッシュ値を取得したいときはこちら
SRI Hash Generator