最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - localize_script or rest api

programmeradmin2浏览0评论

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 badge
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论