I have defined URL in wp-config file.
DEFINE('URL', 'google.com');
Now i want to access this URL from my JS theme file:
<script> alert(URL); </script>
How to do this?
use localize in functions.php
add_action('wp_enqueue_scripts' , function(){
wp_localize_script('jquery', 'config_var', URL );
});
and in js file => config_var will equal the config variable value
You have to put a bit of your JS in your PHP file (i.e. use script tags to accomplish this). Then do the following:
<script> alert(<?php echo URL ?>); </script>If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With