If I were you, I’d store the GPS coordinates in the post meta. Maybe display a meta box on the edit pages, if you want to change it. Otherwise, add the post meta once and forget about it.
Then you can use the wp_footer
hook to print the javascript with the post meta value as the coordinate.
namespace StackExchange\WordPress;
add_action( 'wp_footer', __NAMESPACE__ . '\wp_footer' );
function wp_footer() {
echo '<script>var map = new mapboxgl.Map('.
wp_json_encode( [ 'container' => 'map', 'center' => \get_post_meta( \get_the_ID() 'gpsCoordinates', true ) ])
.');</script>';
}
If there’s already a named script enqueued that contains the code for mapboxgl
, then you can just add an inline script.
namespace StackExchange\WordPress;
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . 'wp_enqueue_scripts' );
function wp_enqueue_scripts() {
wp_add_inline_script( 'mapbox',
'var map = new mapboxgl.Map('.
wp_json_encode( [ 'container' => 'map', 'center' => \get_post_meta( \get_the_ID() 'gpsCoordinates', true ) ])
.');'
);
}
Have a look at the documentation for how to add meta boxes, if you don’t know already.