EDIT: Changed the code to be used in a plugin.
Okay, with the info you provided, you can do something like this:
Step 1) Put your Javascript Map Loading stuff into a .js file. Save it somewhere in your plugin directory, like “myplugin/mapscript.js”
Step 2) create a file in your plugin directory, like “myplugin/myplugin.php”.
Insert the Plugin header so that it can be recognized by wordpress:
<?php
/**
* Plugin Name: My Superawesome Map stuff Plugin
* Description: Does Maps and Stuff
* Version: 1.0
* Author: YOUR_NAME
* License: GPL2
*/
Insert a function in your plugins myplugin.php, where you register & enqueue the script.
function mapstuff_load_scripts() {
if(is_singular()) {
global $post;
wp_enqueue_script('mapstuff-script', plugins_url( 'mapscript.js', __FILE__ ));
wp_localize_script('mapstuff-script', 'mapstuff_script_vars', array(
'lat' => get_post_meta($post->ID,'lat',TRUE),
'lng' => get_post_meta($post->ID,'lng',TRUE),
'addrimg' => get_post_meta($post->ID,'addrimg',TRUE)
)
);
}
}
add_action('wp_enqueue_scripts', 'mapstuff_load_scripts');
I added the conditional is_singular to ensure this is only loaded on post/page view, not on search and index pages.
Step 3) Add the code for a shortcode in the plugin, so you can insert it on your page/post/wherever
add_shortcode('awesomemap','do_awesome_map');
function do_awesome_map($args = array()){
$returnage = "THE STUFF THAT NEEDS TO BE INSERTED FOR THE MAP TO LOAD";
return $returnage;
}
Step 4) Your localized vars will be in an javascript array, so change your mapscript to use these vars like this:
alert(mapstuff_script_vars.lng);
alert(mapstuff_script_vars.lat);
alert(mapstuff_script_vars.addrimg);
Step 4) Upload, insert the shortcode [awesomemap] in your page/post, success ! 😉
Happy Coding,
Kuchenundkakao