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?) ?
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?) ?
Share Improve this question asked Jul 23, 2019 at 10:35 fogxfogx 1011 bronze badge1 Answer
Reset to default 1You'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.