If the data is available in both options, in what use cases should i use the localize script to make data available to my javascript code, and in what cases should i use the rest_api?

I have a custom post type that has meta data which needs to be passed to my php code. I have the option to localize my script and use the data directly, or to call the rest-api and pull the information through ajax.

Which of these two is considered best practice/ which would you advise to use? Or should i use both (to authenticate the api call?) ?

1 Answer 1


You'd only use the REST API or AJAX if you didn't know what data you needed until the script was run or there was some user interaction. If you already know the data you need based on page you're on then you might as just pass that data with wp_localize_script(), otherwise you're forcing the client to make a separate request once the page has loaded, slowing everything down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.