Thanks to visit codestin.com
Credit goes to developer.wordpress.org

apply_filters( ‘wp_inline_script_attributes’, array<string, , string $data )

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

VersionDescription
5.7.0Introduced.

User Contributed Notes

  1. Skip to note 3 content
    Anonymous User

    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)

    function wpdocs_add_nonce_to_scripts( $attr ) {
    	if ( 'text/javascript' !== $attr['type'] ) {
    		return $attr;
    	}
    
    	return array(
    		'type' => 'text/javascript',
    		'nonce' => '123',// Your Nonce. Obviously more featured than this example.
    	);
    }
    add_filter( 'wp_inline_script_attributes', 'wpdocs_add_nonce_to_scripts' );

    Then, you can use 'nonce-123' in your CSP Policy, example:
    "script-src 'self' 'noncoe-123';"

  2. Skip to note 4 content

    I use the following to set my nonce token on inline scripts within WordPress.

    function wpdocs_add_nonce_to_scripts( $atts ) {
        $atts['nonce'] = 'MyNonceToken';
        return $atts;
    }
    add_filter( 'wp_inline_script_attributes', 'wpdocs_add_nonce_to_scripts' );

    Then, when our web server (in our case, nginx) serves the page, we use text replacement to replace MyNonceToken with the page request ID, giving us a unique nonce token for each request. Lastly, we print the Content-Security-Policy header with the request id as the nonce token.

You must log in before being able to contribute a note or feedback.