0

My plugin requires me to save query data for some time. The data isn't big as such; just a couple of search parameters entered by the user so that I can use them across pagination.

I've figured out following ways to do this:

  1. Setting up session_start() either in wp_config OR hooking it up to 'init' and then storing my data in $_SESSION. Then destroying the session when user logs out

  2. Using either set_transient or wp_cache_set; which will store the data in the database; but with expiration time. Of course, I can have my code delete the transient to avoid bloat in the database.

Question: Are the transients shared by all the users visiting a site? So let's say user1 sets transient data; will it be available to user2?

  1. Use Cookies. This perhaps is the same as using $_SESSION; but I'm wondering if there'd be any security issues with this.

Which approach do you suggest? If there's any other more efficient approach - I'd like to know about it as well.

Thank you in advance!

  • 1
    transients are unique if you give them unique names. you will load the same transient for all users if you request the same transient name for all users. – Milo Dec 30 '16 at 6:12
  • If you were to pick any one option from above, which one would you pick? – TheBigK Dec 30 '16 at 8:30
  • 1
    There are no security risks to save the query in the cookie until you don't specify the database name, username, password or other sensitive information. Cookies do not involve communication and server processing, so it should be a bit faster. – Max Yudin Dec 30 '16 at 9:39
  • Using a transient in this case is a bad idea. You can read here: Everyone seems to misunderstand how transient expiration works, so the long and short of it is: transient expiration times are a maximum time. There is no minimum age. Transients might disappear one second after you set them, or 24 hours, but they will never be around after the expiration time. – nmr Jul 30 '19 at 7:29
2

set_transient() using wp_cache_set() and mysql database. WP Cache API using $GLOBAL(global session for application).

Cookies and Session saves data only for one current user(cookies in browser, sessions on backend).

I think better using set_transient(), it has nice hooks and save all data global, even on site disabled cache.

| improve this answer | |

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.