Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI wrote this simple client in javascript that uses mapbox geocoding API. It is not necessary to hide the API key but what is the correct way of giving that js file access to the API key without hardcoding it?
Is it a good solution to embed the API key in the html file in a hidden div and have js read it from that div?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI wrote this simple client in javascript that uses mapbox geocoding API. It is not necessary to hide the API key but what is the correct way of giving that js file access to the API key without hardcoding it?
Is it a good solution to embed the API key in the html file in a hidden div and have js read it from that div?
Share Improve this question asked Aug 9, 2021 at 21:21 Dangz1Dangz1 11 bronze badge1 Answer
Reset to default 2Assuming you are using wp_enqueue_script to load the .js, then use wp_localize_script to set the value of your key.
Another option is to use wp_add_inline_script instead of wp_localize_script
.