I'm working with a wordpress theme, in the admin control panel I' ve added settings for the theme, here I can write some variables, like longitude and latitude for a map (works), and an email, that I need for a contact form, but this doesn' t work, I suppose that the problem is that I use

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/contact-form/contactform.js"></script>

In fact if I do the same with the map, that now is

<div id="gmapp"></div>
            <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
            <script type="text/javascript">
                var lat;
                var lng;
                lat=<?php echo($GLOBALS['desklab_theme_settings']['latcord_text']);?>;
                lng=<?php echo($GLOBALS['desklab_theme_settings']['lngcord_text']);?>;
                var latlng = new google.maps.LatLng(lat,lng);

                var options = {
                    zoom: 15,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };  

                var map = new google.maps.Map(document.getElementById('gmapp'), options);

                var marker = new google.maps.Marker(
                {
                    position: latlng,
                    map: map
                }
                );

            </script>

I tried to write so:

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/contact-form/map.js"></script>

But I don't see the map, and variables aren't found.

So what should I do to get the variable from settings as I do with lat and lng? I should write the js code in the php as I did with map?

share|improve this question
You seem to have a stray <?php endif; ?> from which i don't see the if part. – jaudette Dec 9 '12 at 12:13
Yes you're right, but I' ve already corrected that – AlessioMTX Dec 9 '12 at 12:15
and I still have problem – AlessioMTX Dec 9 '12 at 12:16
Just to make sure, you are trying to move the second script tag into map.js, is that it? – jaudette Dec 9 '12 at 12:17
1  
They don't because your php engine does not process your .js files, they are passed as-is to your browser. – jaudette Dec 9 '12 at 12:21
show 1 more comment

4 Answers

up vote 1 down vote accepted

You should move part of your code to the map.js file:

//map.js
var generateMap = function(lat,lng) {
  var latlng = new google.maps.LatLng(lat,lng);
  var options = {
         zoom: 15,
         center: latlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP
  };  

  var map = new google.maps.Map(document.getElementById('gmapp'), options);

  var marker = new google.maps.Marker(
      {
         position: latlng,
         map: map
       }
  );
}

and from your php, do

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/contact-form/map.js"></script>
<script type="text/javascript" >
  var lat=<?php echo($GLOBALS['desklab_theme_settings']['latcord_text']);?>;
  var lng=<?php echo($GLOBALS['desklab_theme_settings']['lngcord_text']);?>;
  generateMap(lat,lng);
</script>

See this SO question for other solutions regarding data passing from PHP to JS

share|improve this answer
Ok I'm trying ;) – AlessioMTX Dec 9 '12 at 12:28
So works :D thank you!!! Now I try to do the same for the email ;) – AlessioMTX Dec 9 '12 at 12:32

Don't forget echo

<script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/contact-form/map.js"></script>
share|improve this answer
I didn't use echo, now I try – AlessioMTX Dec 9 '12 at 12:23
No difference. Look wpthemes.mtxlab.us/test/?page_id=50 – AlessioMTX Dec 9 '12 at 12:26
As you can see with developer tools of google, the js file is found and recognized, but doesn't take variable lat and lng – AlessioMTX Dec 9 '12 at 12:27

You need to rename your map.js file to something like map.php so that the PHP engine parses it. Then your source will be:

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/contact-form/map.php"></script>

The contents will still be javascript with your PHP variables in place.

share|improve this answer
Now I try also your solution – AlessioMTX Dec 9 '12 at 12:33

One suggestion. Instead of using bloginfo('template_url') you should start using get_template_directory_uri() as the previous is being deprecated.

PS. I do not have comment privilege unless I answer a post.

share|improve this answer
Ok thank you I'll change it – AlessioMTX Dec 9 '12 at 13:57

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.