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

ajax - Is there a better way to access transients using javascript

programmeradmin1浏览0评论

I've been searching around the web trying to find a better way to use WordPress transients with javascript. Currently I am using admin-Ajax to run a php function that retrieves the transient and returns it. This works fine but I'm curios to know if there is a more effective way of accessing that data without the overhead of using ajax and php.

I know I could probably do this using wp_localize_script() but I'm not sure if that method is any better that my current solution. Hopefully there is an expert out there that can help me with this.

I've been searching around the web trying to find a better way to use WordPress transients with javascript. Currently I am using admin-Ajax to run a php function that retrieves the transient and returns it. This works fine but I'm curios to know if there is a more effective way of accessing that data without the overhead of using ajax and php.

I know I could probably do this using wp_localize_script() but I'm not sure if that method is any better that my current solution. Hopefully there is an expert out there that can help me with this.

Share Improve this question edited Aug 4, 2017 at 9:19 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Dec 15, 2015 at 5:52 Josh wJosh w 212 bronze badges 2
  • 1 Which method is the best depends on what the purpose is. What the script does? How do you use the data from transients in JavaScript? In general, if the transient is very small in size and it is always used in the page, I would use wp_localize_script() (or any proper method to send the data within the document). – cybmeta Commented Dec 15, 2015 at 7:29
  • I'm using system cron to collect external data and store it every 2 minutes. It then builds a wp query relating to the new data and stores it in a transient. On the front end, I have a feed that auto refreshes every 30 seconds. I'm using js to compare the transients for changes. If they are duplicates I can skip the need to rebuild the page. If they don't match i.e. the transient is different or it is expired or broken, I rebuild the page. What I'm doing is reducing unnecessary work by detecting duplicates. I'm just not sure if this is the best way to do that. – Josh w Commented Dec 16, 2015 at 0:27
Add a comment  | 

1 Answer 1

Reset to default 1

Transients should be used for short (for varying definition of it) time caching, and they are by design not reliable. From your description it sound like you are using them in a manner they were not designed to support and therefor probably the "best" for you is to not use transients at all.

发布评论

评论列表(0)

  1. 暂无评论