[英]Pass PHP variables to Javascript
我已經將自定義Google Maps腳本實現為Wordpress短代碼,但是我對.js
文件中定義但Wordpress通過PHP生成的某些值有.js
這是.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>
將所需的PHP變量回顯到主題文件中的Javascript變量中,並在腳本文件中引用JS變量。
主題文件:
<script>
var config_theme_directory = "<?=get_template_directory()?>";
</script>
包含的JS文件:
console.log( config_theme_directory );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.