Viewing 9 replies - 1 through 9 (of 9 total)
  • You probably need to know the name of the nonce that script handle is using. Nonce can be created with wp_create_nonce()

    Example:

    wp_add_inline_script( 
    	'script-handle',
    	'const my_script_params = ' . json_encode( 
    			array(
    				'ajax_url'   => admin_url( 'admin-ajax.php' ),
    				'my_nonce'   => wp_create_nonce( 'my_nonce' )
    			)		
    	),
    	'before' 
    );

    Thread Starter Lukman Nakib

    (@pyrobd)

    Actually I am not adding a new inline script, I already a registered a script source then added some variables using wp_localize_script() for that script. Then the localized variable are loaded as inline script and I can not add any nonce to it, I can add nonce or attributes to main registered script thought, please check my screenshot.
    Hope you understood the issue.

    Moderator bcworkz

    (@bcworkz)

    The way to add a nonce value to inline script is through wp_localize_script() or wp_add_inline_script() like Sanjeev suggested. They both work in a similar manner. Since you already use wp_localize_script(), just add a nonce value to the list of passed vars. These are global values, so it doesn’t matter much which script handle you use to localize. It does have to be a valid handle enqueued for the page.

    The nonce value does need to be coordinated with the server script that validates the nonce. The $action arg passed to wp_create_nonce() has to match that which the validation script is using.

    Thread Starter Lukman Nakib

    (@pyrobd)

    I understood what you are saying , but I need to add this as a script tag attribute, if I do as you say, it will work the global values will be inside the script ,as script content but what I need is a script tag attribute like this
    <script my-custom-attr="my-value"> //content </script>

    The way your are suggesting will work like the following
    <script > my-custom-attr="my-value" </script>

    Hope it is clear now, could find a way for this anywhere.

    Thanks again

    Moderator bcworkz

    (@bcworkz)

    You can use the ‘wp_script_attributes’ to add extra script tag attributes. Your callback is passed an array of any other attributes already in place, which should include the ID attribute for the specific script tag you are targeting.

    Thread Starter Lukman Nakib

    (@pyrobd)

    Thank you but I already found this and the problem is ?‘wp_script_attributes’? it does not get called when I use wp_localize_script function or registering a script. So it does not work.

    Moderator bcworkz

    (@bcworkz)

    Hmmm… That filter doesn’t seem to be used at all. I wonder what that’s about.

    Anyway, you could output the entire HTML script block from the “wp_print_scripts” action. Doing it this way means any dependencies cannot be properly managed, but by using appropriate $priority values when adding your callback, you can at least manage whether the output occurs early or late.

    Thread Starter Lukman Nakib

    (@pyrobd)

    Should not ??‘wp_script_attributes‘ filter should be able to filter script tag attributes, if not why not add the support for it, if it does not get called up I am confused about the use this filter

    Moderator bcworkz

    (@bcworkz)

    One would think. It’ll take some digging to figure out where it actually is used, or more importantly, why it’s not used on many scripts. It might be an actual bug, but I don’t have time right now to investigate further (I’m in travel mode, sitting at a departure gate).

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding Nonce in Extra js script’ is closed to new replies.