将wordpress $ current_user-> ID从php文件发送到外部Javascript文件 [英] Send wordpress $current_user->ID from php file to external Javascript file
问题描述
我试图根据登录的伙伴新闻用户将KML文件动态加载到地图中.当我在javascript文件中对userID进行硬编码时,一切正常,但是我需要它来动态加载当前用户的用户ID
I am trying to dynamically load a KML file into a map depending on which buddy press user is logged in. Everything works fine when I hardcode a userID in the javascript file, but I need it to dynamically load the current user user ID.
这是JavaScript文件:
Here is the javascript file:
function initialize(){
//MAP
//manually set user ID to zero
//var current_user =0;
var current_user;
//var current_user = "<?php get_currentuserinfo(); echo $current_user->ID?>"
var latlng = new google.maps.LatLng(90.64138205695923,-45.38903528363647);
var options = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
var kmlLayer = new google.maps.KmlLayer('http://www.mysite.com/timeline_'+current_user+'.kml');
kmlLayer.setMap(map);
这就是我在php文件中的循环内的内容:
This is what i have in the php file, within the loop:
$("#address1").click( function() {
initialize();
var currentUser = <?php get_currentuserinfo(); echo $current_user->ID?>;
$current_user = currentUser;
});
就像我提到的那样,如果我对用户ID进行硬编码,它可以工作,但是我不确定如何将用户ID从PHP传递到javascript文件.
Like I mentioned, it works if i hard code the user ID, but I am not sure how to pass the UserID from PHP to the javascript file.
推荐答案
正如我之前的评论所建议的,我认为这将解决您的问题:
As my earlier comment suggested, I think this will solve your issue:
在js文件中:
...
function initialize(current_user) {
...
}
...
在php中:
...
<script type="text/javascript">
$("#address1").click( function() {
initialize(<?=json_encode($current_user->ID)?>);
});
</script>
...
这篇关于将wordpress $ current_user-> ID从php文件发送到外部Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!