Using PHP in Google map shortcode to get address (2 posts)

  1. justin_h
    Posted 4 years ago #


    I have a [map] shortcode in my sidebar.php file to display a google map.

    <?php echo do_shortcode(' [map address="123 Main Street"]
    '); ?>

    That works fine, but what I want to do is put a <?php in the address area, because I want it to automatically populate using the address fields I have created.

    <?php echo do_shortcode(' [map address="<?php stripslashes($options['address_city']);  ?>"]
    '); ?>

    [address_city] is a field located in the admin panel, so my clients to enter their details. The code for this can be found in mytheme/functions/theme_admin.php and looks like this;

    <h3><?php _e( 'City' ); ?></h3>
    <input id="complete_theme_settings[address_city]" class="regular-text" type="text" name="complete_theme_settings[address_city]" value="<?php esc_attr_e( $options['address_city'] ); ?>" />

    Thanks for your help!

  2. justin_h
    Posted 4 years ago #

    Hello again, I have managed to solve this!

    No plugins needed, I just attached the standard javascript code in my header.php and then went on to add this to my sidebar-page.php:

    <body onunload="GUnload()"> 
        <div id="map_canvas" style="width: 250px; height: 250px"></div> 
        <script type="text/javascript"> 
        var yourAddress = '<?php echo stripslashes($options['address_country']);  ?>';
        if (GBrowserIsCompatible()) {
           var geocoder = new GClientGeocoder();
           geocoder.getLocations(yourAddress, function (locations) {
              if (locations.Placemark)
                 var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
                 var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
                 var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
                 var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;
                 var bounds = new GLatLngBounds(new GLatLng(south, west),
                                                new GLatLng(north, east));
                 var map = new GMap2(document.getElementById("map_canvas"));
                 map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

    More info can be found here: http://stackoverflow.com/questions/2297486/render-google-map-in-wordpress-based-on-address-custom-field

Topic Closed

This topic has been closed to new replies.

About this Topic