Filters attributes to be added to a script tag.
Parameters
string|bool> $attributes Key-value pairs representing<script>tag attributes.
Only the attribute name is added to the<script>tag for entries with a boolean value, and that are true.$datastring- Inline data.
Source
$attributes = apply_filters( 'wp_inline_script_attributes', $attributes, $data );
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |
This has to be used to make WP a CSP compliant system (at least, in the front end. Remains to be tested in the admin area)
Then, you can use
'nonce-123'in your CSP Policy, example:"script-src 'self' 'noncoe-123';"$attr[‘nonce’]on the existing array rather than return a new array.I use the following to set my nonce token on inline scripts within WordPress.
Then, when our web server (in our case, nginx) serves the page, we use text replacement to replace
MyNonceTokenwith the page request ID, giving us a unique nonce token for each request. Lastly, we print theContent-Security-Policyheader with the request id as the nonce token.