If you’re in JS you can’t use PHP (<?php ?>
), you need to another way to get a PHP variable into JS. The normal way with WordPress is to use wp_localize_script()
.
wp_localize_script( 'wpse_274344', 'myScriptObject', array(
'markerImageUrl' => get_theme_file_uri( '/img/mapmarker.png' ),
) );
Add that code after your call to wp_enqueue_script()
or wp_register_script()
. Replace wpse_274344
with the name you used when enqueuing the script.
This means that WordPress will output a JavaScript object named myScriptObject
to the page containing the values that you passed to wp_localize_script()
.
Then in your script file you can access the markerImageUrl
like this:
markerImage = {
url: window.myScriptObject.markerImageUrl
}