0

I'm very new to PHP. On a Wordpress site, I want to use custom Google Maps with the Javascript API V3. Some posts will have maps in them, and I want to use PHP variables to determine the unique locations, zoom level, info window text, etc. The Google Maps javascript will be in the footer with all the styles, map type and anything that will be consistent from map to map.

So for posts which have a map, written in the post there will be something like:

<?php
$location = "-34.397, 150.644";
$zoom = "8";
?>

<div id="map"></div>

And in the javascript in the footer theme file will be something like:

<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(<?=$location;?>);
    var myOptions = {
      zoom: <?=$zoom;?>,
          center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map"),
        myOptions);
      }
</script>

Plus all the other crap that Google requires.

This works fine if I put it in any other php file, but not within Wordpress. What am I doing wrong?

1 Answer 1

1

By default, PHP is stripped out of the posts content. You can use this plugin to aleivate this: http://wordpress.org/extend/plugins/exec-php/

This will give you a quick, easy fix to be able to do it the way you are trying to. I would suggest reading http://codex.wordpress.org/Custom_Fields for the more proper way to do this though.

1
  • That plugin is good to know about, thanks. I installed it, and tested it, it works but still does not make the variables work. So is there something wrong with having PHP in the footer? Or how I wrote it within the javascript? Dec 15, 2011 at 13:49

Your Answer

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

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