将PHP变量传递给Javascript

将PHP变量传递给Javascript,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我已经实现了一个自定义的Google地图脚本作为Wordpress短代码,但是我对.js文件中定义的一些值有一个问题,这些值是Wordpress通过PHP生成的 这是.js文件: //set your google maps parameters var latitude = 41.03328, longitude = 21.30281, map_zoom = 16; //google map custom marker icon - .png fallback for IE1

我已经实现了一个自定义的Google地图脚本作为Wordpress短代码,但是我对
.js
文件中定义的一些值有一个问题,这些值是Wordpress通过PHP生成的

这是
.js
文件:

//set your google maps parameters
var latitude = 41.03328,
    longitude = 21.30281,
    map_zoom = 16;

//google map custom marker icon - .png fallback for IE11
var is_internetExplorer11= navigator.userAgent.toLowerCase().indexOf('trident') > -1;
var marker_url = ( is_internetExplorer11 ) ? 'img/cd-icon-location.png' : 'img/cd-icon-location.svg';

//define the basic color of your map, plus a value for saturation and brightness
var main_color = '#00e1ff',
    saturation_value= -20,
    brightness_value= 5;
我需要做的是获取联机图像的Wordpress主题目录:

'img/cd-icon-location.png' : 'img/cd-icon-location.svg'
对于
var main_color='#00e1ff'
,从PHP获取该值

<?php echo oneengine_option( 'main_color' ); ?>

我通常使用PHP获取JS值的方法是在导入JS脚本或编写的代码之前声明它们

<?php
    echo '<script type="text/javascript">var main_color = "'.$main_color.'";</script>';
?>

<script type="text/javascript" src="someJSScript.js">
    //If not in separate file, JS code will go here
</script>

//如果不是在单独的文件中,JS代码将出现在这里

将需要的PHP变量回送到主题文件内的Javascript变量中,并引用脚本文件内的JS变量

主题文件:

<script>
    var config_theme_directory = "<?=get_template_directory()?>";
</script>

您可以从页面元素或CSS规则中读取颜色值。您应该将其包含在正确的位置,并在加载整个页面后读取变量,否则您可能会遇到未定义变量的问题。我尝试的所有操作都会返回未捕获的引用错误:$templateDir未定义或未检测到变量。我在发疯,我讨厌这种乱七八糟的通心粉式的语言
console.log( config_theme_directory );