Home › Forums › Front-end Issues › Passing the_field() as a JS variable › Reply To: Passing the_field() as a JS variable
I would use wp_localize_script https://codex.wordpress.org/Function_Reference/wp_localize_script
You could also just echo the values into a script tag on the page, but localizing the script would be the preferred method for an external script.
<script>
// echo a php value into a js variable on page
var map_url = '<?php the_field('map_url'); ?>';
</script>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.