[英]Pass PHP variables to Javascript
I've implemented a custom Google Maps script as a Wordpress shortcode but I have a problem with some of the values that are defined in the .js
file but generated by Wordpress through PHP 我已经将自定义Google Maps脚本实现为Wordpress短代码,但是我对
.js
文件中定义但Wordpress通过PHP生成的某些值有.js
This is the .js
file: 这是
.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;
What I need to do is get the Wordpress theme directory for the images on line: 我需要做的是获取在线图像的Wordpress主题目录:
'img/cd-icon-location.png' : 'img/cd-icon-location.svg'
and for var main_color = '#00e1ff'
, get this value from PHP 对于
var main_color = '#00e1ff'
,从PHP获取此值
<?php echo oneengine_option( 'main_color' ); ?>
What I usually do to get JS values using PHP is declaring them before importing the JS script or the written code 我通常使用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>
Echo the PHP variables you need into Javascript variables inside of your theme files and reference the JS variables inside of your script files. 将所需的PHP变量回显到主题文件中的Javascript变量中,并在脚本文件中引用JS变量。
Theme files: 主题文件:
<script>
var config_theme_directory = "<?=get_template_directory()?>";
</script>
Included JS file: 包含的JS文件:
console.log( config_theme_directory );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.