WordPress Google Maps in Custom Theme

Enqueue your script and use wp_localize_script to pass data.

function wpd_scripts() {

    wp_enqueue_script(
        'wpd_script',
        get_stylesheet_directory_uri() . '/js/script.js',
        null,
        null,
        false
    );

    wp_localize_script(
        'wpd_script',
        'WPD',
        array(
            'stylesheet_dir' => get_stylesheet_directory_uri()
        )
    );

}
add_action( 'wp_enqueue_scripts', 'wpd_scripts' );

You can then use WPD.stylesheet_dir in your script to get the value you set in PHP.

var marker_url = WPD.stylesheet_dir + '/assest/imgs/map/cd-icon-location.png';

Leave a Comment