2

I have two hooks in separate HTML request.

I have to pass some data i.e. variable from one to the other.

What is the most practical and transparent method for it?

3

It's not possible to pass data from one HTTP request to another one on the fly. Once the script is finished, the data will be discarded.

What you can do is to store the data in a transient, and then retrieve it later. Here's a simple example using set_transient():

set_transient( 'my_transient', $data, 1 * HOUR_IN_SECONDS );

Then, you can retrieve it using get_transient():

get_transient( 'my_transient' );
  • Thank you Jack, this is exactly the answer to what I asked. The only problem, that we have to create a fallback, because we can lost the stored data before the expiration time quoting Ryan McCue: "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." Do you have some advice or experience for this case? – Geeocode Mar 11 '18 at 2:33
  • If you are concerned about the expiration, you can always use the Options API instead. Transient is more likely for temporarily data storage. – Jack Johansson Mar 11 '18 at 3:22
  • hmmm, this can work if you have a user ID (logged in user) which you can use to separate data cumming from different users. If you need to support non logged user this will end in users getting data from other users. – Mark Kaplun Mar 11 '18 at 10:43
1

in the very general case, you just can not. Two http requests are fully independent from each other, and in case of big site might not even be processed by the same server.

What you need to do is to "force" the second request to carry information that will help you identify or extract the relevant values when processing it.

The most basic mechanism for that is cookies, but that is not the only one, and if for example you handle multi page form, you can simply include it as hidden input in the next form page you generate, and if the whole thing is done via AJAX, you can add state information in a reply to the first request, store it in some global space, and attach it to the next request.

  • Thank you for your answer Mark. Yes the hidden form field method is my current solution for it, but it requires additional actions I try to avoid to make the code as transparent as it possible. I just wondered if there is any other solution. Anyway for the useful answer I give you at least a thumb up. – Geeocode Mar 11 '18 at 16:17
  • The problem with server side methods, as I explained in the comment on jack's answer, is that you need some kind of unique identifier to avoid creating a mix-up, if the user is not logged in you can try to use sessions, but that again will not work on multi server site, and will still use a cookie. There is just no way around having the client to carry and send some kind of unique identifier. – Mark Kaplun Mar 11 '18 at 16:52

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.