2

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?

6
  • You seem to have a stray <?php endif; ?> from which i don't see the if part. Commented Dec 9, 2012 at 12:13
  • Yes you're right, but I' ve already corrected that Commented Dec 9, 2012 at 12:15
  • and I still have problem Commented Dec 9, 2012 at 12:16
  • Just to make sure, you are trying to move the second script tag into map.js, is that it? Commented Dec 9, 2012 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. Commented Dec 9, 2012 at 12:21

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Now I try also your solution
1

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.

Comments

0

Don't forget echo

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

3 Comments

I didn't use echo, now I try
As you can see with developer tools of google, the js file is found and recognized, but doesn't take variable lat and lng

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.