Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Nonces

  • Hi
    I searched the forum but can’t see nothing related to this, so I openen this topic.

    I’m trying to add nonces to js files and the inlined one. For the inlined I used this:

    add_filter('wp_inline_script_attributes', 'add_nonce_to_inline',10,1);
    
    function add_nonce_to_inline ($attributes, $data) {
        
        $attributes['nonce'] = 'NGINX_NONCE_VALUE_1';
        
        return $attributes;
        
    }

    It worked well, unless in a GP theme snipet, this one:

    <script id="generate-a11y">

    I see in the code it is inlined with php prinf, so maybe is out of the scope of wp_inline_script_attributes.

    How I can add the nonce to the snippet?

    Thanks in advance

  • Hi there,

    Try this:

    add_filter( 'generate_print_a11y_script', '__return_false' ); // Disable the default script
    
    function custom_generate_a11y_script() {
        printf(
            '<script id="generate-a11y" nonce="%s">%s</script>',
            'NGINX_NONCE_VALUE_1', // Replace with your nonce value
            '!function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}();'
        );
    }
    add_action( 'wp_head', 'custom_generate_a11y_script',10 );
  • Hi Ying.
    Thanks, it worked.
    Maybe the dev team must think about how this code is injected and make it parseable with the wp_inline_script_attributes functions, so no aditional code is needed.

    Talked too fast… now it throws error about Cannot read properties of null (reading ‘addEventListener’)

  • Hi there,

    try this snippet instead:

    
    remove_action( 'wp_footer', 'generate_do_a11y_scripts' );
    add_action( 'wp_footer', 'custom_do_a11y_scripts' );
    
    function custom_do_a11y_scripts() {
    	if ( apply_filters( 'generate_print_a11y_script', true ) ) {
    		// Add our small a11y script inline.
    		printf(
    			'<script id="generate-a11y">%s</script>',
    			'!function(){"use strict";if("querySelector"in document&&"addEventListener"in window){var e=document.body;e.addEventListener("mousedown",function(){e.classList.add("using-mouse")}),e.addEventListener("keydown",function(){e.classList.remove("using-mouse")})}}();'
    		);
    	}
    }
    

    ill have a chat with the devs about why we hook it in the way we do….

  • Hi David
    This worked without error, nonce is added

  • Glad to hear that!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.