Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 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.

share|improve this answer
    
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? –  Scott Chandler Dec 15 '11 at 13:49

Your Answer

 
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.