1

I'm making an Ajax call in an Elementor site, and I'd like to use a nonce while doing it. Since my code is really simple, I didn't create a new JS file for it placed in Elementor's HTML widget, and I handle the request in the functions.php file.

I realized that the best way is to use wp_localize_script to make both the nonce and the Ajax URL available in the client side, but the function is asking for a file to register, and I don't know how to approach this, since there is no file to register and enqueue, functions.php is probably already enqueued?

How is it done? I've already read through so many tutorials and still didn't find that answer.

Any knowledge will be of great help. Many thanks.

1 Answer 1

1

wp_localize_script() doesn't care much which script handle you use as long as it is a valid one. You could use your theme's or Elementor's main script handle with the function to make the data available for your script. You'll need to check your theme's or Elementors source coude to find what these handles are.

Side note: It says on More information on the documentation that it is nowadays recommended to use wp_add_inline_script() to provide data to scripts as

wp_localize_script() should only be used when you actually want to localize strings.

2
  • that makes so much sense! thank you! I would think I must use Elementor's main script handle for the data to be available for the scripts that are scattered over the site using the html widget. otherwise? how else it will be exposed for usage there? right? or is it globally available? no matter which script defined it?
    – yaara
    Apr 14, 2022 at 8:04
  • @yaara Yes, Elementor's main script file handle would probably make the most sense so that the added data will be available when ever Elementor scripts are loaded and the html widget is used. Apr 14, 2022 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.