WordPress allows you to use PHP variables in JavaScript by using the wp_localize_script function
. This makes it possible to use data generated on the server side in client-side scripts.
wp_localize_script('my-script', 'my_script_vars', array('var1' => 'value1', 'var2' => 'value2'));
The above code adds variables named ‘var1’ and ‘var2’ to the script named ‘my-script’. Each variable is assigned the values ‘value1’ and ‘value2’. These variables can be accessed in JavaScript as ‘my_script_vars.var1’ and ‘my_script_vars.var2’.