I need to pass a plugin’s $atts variable to a javascript function, but I’m not sure how to do it. The javascript function needs the shortcode’s php latitude variable in order to build the map.
My plugin looks like this:
<?php
/*
Plugin Name: Lax Google Map Plugin
Plugin URI: http://www.mysite.com
Description: Makes Map
Version: 1.0
Author URI: http://www.mysite.com
*/
function lax_google_map_init() {
wp_enqueue_script('google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=false');
wp_enqueue_script('lax_google_map_script', plugins_url('js/lax_google_map_script.js', __FILE__), array('google-maps','jquery'));
}
add_action('init', 'lax_google_map_init');
function lax_google_map_maker($atts,$content=null) {
$atts = shortcode_atts( array(
'width' => '600',
'height'=>'600',
'latitude'=>'38.9205'
),
$atts);
$output .= '<div id="map_canvas" style="width:'. $atts['width'] .'px; height: '. $atts['height'] .'px; border:1px solid black;"></div>';
return $output;
}
add_shortcode('lax-google-map', 'lax_google_map_maker');
?>
The javascript file looks like:
jQuery.noConflict();
jQuery(document).ready(function($) {
var latlng = new google.maps.LatLng(39.73, -76.02);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
});
So I need to replace ‘39.73’ with something like:
pseudo-code:
var latlng = new google.maps.LatLng( + $atts[‘latitude’] + , -76.02);
Also, the javascript gets loaded before the shortcode $atts are available. What’s the best way to change it?
Sorry, for the basic questions. I’m learning how to write a plugin.
Any suggestions?
Thank you.
-Laxmidi
UPDATE:
If I add this:
$params = array (
'latitude'=> '39.01',
'longitude'=> '-76.02'
);
wp_localize_script('lax_google_map_script', 'lax_map_params', $params);
to lax_google_map_init()
And I add:
var latlng = new google.maps.LatLng( lax_map_params.latitude, csf_crime_map_params.longitude);
to my javascript function, the map works. How would I replace ‘39.01’ with the $atts[latitude]
and -76.02 with $atts[longitude]
? I’m still learning about scope in php. Thank you.
You need to have some initial (maybe global) js
var markers_array = [];
. Then use an ajax callback function to bump your markers into that array.Register your scripts
The initial Markers
The Ajax Callback function
The data handling inside ajax_json.js
Now you can write some
document.ready()
function inside your new ajax json handling file. In there you can access your data via theajax_object
javascript object. You could use$.post()
to perform data handling via forms, or else. You can also bump your markers into somecreateMarkers
js function which you could also use to build your initial array (you need to handle the marker creation somewhere.Why you shouldn’t add this data as mark-up element attributes
Mark up get’s rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you’ll need to drop support for device browsers and netbook laptops, tablets, etc.
You could use wp_localize_script to take your PHP variables and put them into JavaScript.
I would do this that way:
in plugin code add data-latitude attr to div#map_canvas:
and in javascript allow JQuery to read attribute data-latitude from div#map_canvas
Probably you will need a similar code snippets for longitude..
use attributes prefixed with “data-” in order to have a html5 validation.
Hope it helps