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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

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 acplish this). Then do the following:

  1. Place the script in your php file
  2. <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