Send wordpress $current_user->ID from php file to

2019-08-11 04:55发布

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.

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);  

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;

                });

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.

1条回答
混吃等死
2楼-- · 2019-08-11 05:04

As my earlier comment suggested, I think this will solve your issue:

In the js file:

...
function initialize(current_user) {
    ...
}
...

In the php:

...
<script type="text/javascript">
    $("#address1").click( function() {
        initialize(<?=json_encode($current_user->ID)?>);
    });
</script>
...
查看更多
登录 后发表回答