Can set_transient() be used in multi-site?

I understand that Transients API is used to cache data and should be intended to store data that is expected to expire.

However, please clarify the following.

Can set_transient() be used to store data in multi-site scenario? If yes, how is it different from using set_site_transient() function?

Topic transient multisite Wordpress

Category Web


As the Codex suggests:

Essentially the same as set_transient() but works network wide when using WP Multisite.

One difference is that the transient name should be 40 characters or less in length. Also, while set_transient() sets transients that have an expiration time to not autoload, all transients added with set_site_transient will auto-load at all times.

In other words, the fundamental difference is in the auto-loading and since transients might be serialized, you could risk breaking things if referring to a blog (in MU) which has different settings/url/etc.

So the short answer is:

  • use set_transient for single blogs

  • use set_site_transient when you need something for ALL blogs.

Example of stored transients:

enter image description here

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.