Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxyudin/8b48c324a40147673ebc40432aca81b1 to your computer and use it in GitHub Desktop.
Save maxyudin/8b48c324a40147673ebc40432aca81b1 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin or functions.php
*/
function mxd_csp_nonce() {
// generate nonce here
return 'sha384-7TB/burIFPND3rU7p38KUmP8C6LhM+Ybd8yzSEb1FvUgyJd9TMJ9HqWP3q+sFZOM';
}
function mxd_wp_script_attributes( $attr ) {
if ( ! isset( $attr['nonce'] ) ) {
$attr['nonce'] = mxd_csp_nonce();
}
$attr['source'] = external;
return $attr;
};
add_filter( 'wp_script_attributes', 'mxd_wp_script_attributes' );
function mxd_wp_inline_script_attributes( $attr ) {
if ( ! isset( $attr['nonce'] ) ) {
$attr['nonce'] = mxd_csp_nonce();
}
$attr['source'] = inline;
return $attr;
};
add_filter( 'wp_inline_script_attributes', 'mxd_wp_inline_script_attributes' );
/**
* footer.php
*/
wp_print_inline_script_tag(
'document.createElement("video");',
array(
'id' => 'create-video-js',
'async' => true,
)
);
wp_print_script_tag(
array(
'id' => 'jquery-"js"-2.3.4',
'integrity' => 'sha384-long_hash',
'src' => esc_url( 'https://domain.tld/jquery-2.3.4.min.js' ),
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment