admin管理员组文章数量:1405531
I have defined URL in wp-config file.
DEFINE('URL', 'google');
Now i want to access this URL from my JS theme file:
<script> alert(URL); </script>
How to do this?
I have defined URL in wp-config file.
DEFINE('URL', 'google.');
Now i want to access this URL from my JS theme file:
<script> alert(URL); </script>
How to do this?
Share Improve this question asked Aug 25, 2018 at 8:56 AdamAdam 5079 silver badges25 bronze badges3 Answers
Reset to default 5use 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 acplish this). Then do the following:
- Place the script in your php file
<script> alert(<?php echo URL ?>); </script>
you can read the php variable inside th js using echo. use like this
var url='<?php echo _URL; ?>';
alert(url);
本文标签: javascriptHow to read global variable from WPConfigphp in JS fileStack Overflow
版权声明:本文标题:javascript - How to read global variable from WP-Config.php in JS file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744889966a2630717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论