8

Are there official guidelines on where a plugin should cache files?

If not, is there a best-practice out there that I can follow?

For example, a plugin grabs content from a back-end system benefits from caching the file so it doesn't repeatedly fetch the same content from the back-end.

Searching here and elsewhere is tough as most queries lead to caching plugins or database tutorials.

My instinct says to put them into a sub-directory of wp-content/uploads.

My secondary instinct would be to keep the files within the plugin's directory tree. This keeps plugin data grouped together but doesn't follow (what appears to be) WordPress architecture where user content is in /uploads.

5

It would be nice if WordPress had a standards based cache directory, but since it doesn't I think the best option is to keep it in the plugin folder itself.

Uploads are in my opinion for actual uploads and cache files are not really considered as such. I think it easier to manage , reduces possible conflicts and to be honest it's where I would look if a plugin has this functionality.

tl;dr: The cache files are part of the functionality of a specific plugin and should be packaged with it.

  • 1
    The problem with this approach is that a lot of people disable write access from anything but the uploads folder. – kraftner Apr 20 '15 at 12:30
  • 1
    When a plugin is updated, WP will delete the old plugin entirely and recreate it from scratch. So even if you set permissions specifically for a cache directory within a plugin, each upgrade will remove not only the cached files, but any trace of those write permissions. – Jason Sep 26 '15 at 22:21
  • I've seen a couple do WP_CONTENT_DIR / cache, and I think I'm going to transition to that after having done a solution in the plugin folder for a while. As Jason said, the data could be unnecessarily lost, and moreover (though not an issue for casual users) the plugin folder size can grow disproportionately, whereas I like to think of that folder as a fair representation of what came out of the box. – Luke Sawczak Apr 4 '17 at 15:57
1

If you don't need to store very large data you should take a look to the Wordpress Transient API:

http://codex.wordpress.org/Transients_API

Your data will be handled by Wordpress with an expiration time. I think it is the more "wordpress-way" to cache data.

  • In this case, the data can be pretty large...but I really like this Transients approach for smaller stuff. Thanks for pointing it out. :) – Matthew Bakaitis Aug 24 '15 at 12:51
0

Sitting with the same "problem". At first i was thinking of store this cache as an option, but my cache simply was too big so this did not work. So maybe that's an alternative if your cache never will be really large. For example if you're communicating with an API and can only do a few requests per hour and that the return answer is not too long. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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