I've written a custom plugin. When page-a on my site is loaded:

  1. My plugin's main PHP prepares data to send to the page. During execution fo this file, a PHP variable is defined and assigned a value ($myvar).
  2. During page load, an AJAX call is made back to my server, which executes a custom PHP callback function.

I would like the PHP callback function to know the value of $myvar. Is this possible? If so, is it advisable, or is it perhaps non-standard and/or risky?

One roundabout way is to send $myvar to the browser so it is accessible via JavaScript (using wp_localize_script()), then send that same value back to the server during the AJAX call. But this seems inefficient, especially since in this case, the browser does not need to know the value of $myvar.

I should say that I have alternative methods of accomplishing what I want, which seem safe and viable. In short, I might be able to simply set the value of $myvar either in the callback function, or possibly even determine its value via JavaScript. So unless I discover a very advantageous solution in this post, I will most likely go with one of those alternative methods. But I just thought I'd ask here first, in case I'm overlooking a viable approach (and to enlighten myself).


  • 1
    If you don't want to expose the PHP variable to the browser (and send the variable when making the AJAX to your custom PHP callback), then you may want to use the transients API.
    – Sally CJ
    Feb 10, 2020 at 14:49
  • 1
    Yes, using PHP sessions would do what you need, but check out Tom's awesome answer here where he talked about persisting data and why should one not use PHP sessions. And here's an in-depth article about PHP sessions.
    – Sally CJ
    Feb 11, 2020 at 21:54
  • 1
    Well IMHO, when you don't mind the page to not be cached and you've taken all the security measures (for things like session hijacking) and those steps that maintain the site's performance and scalability (e.g. using this plugin). And honestly, I've not used PHP sessions for a long time, or that in WordPress, I've always used the transients API for persisting temporary data that shouldn't be made available to the browser.
    – Sally CJ
    Feb 14, 2020 at 14:52
  • 1
    And I don't want to be rude, but if you need a more satisfying answer, you can try asking on Stack Overflow.. :) Also, I suggest you to read this article now.
    – Sally CJ
    Feb 14, 2020 at 14:53
  • 1
    I don't think you're being rude at all. You're providing me with suggestions that I value greatly--thank you. And I don't think I need a more satisfying answer. The info you've given me is very comprehensive. I agree with you--using a transient seems to be the best solution in my case. Do you want to write that up in an answer here so I can mark it as the best answer to this question?
    – cag8f
    Feb 15, 2020 at 10:00

1 Answer 1


Based on comments in this thread, I think using a WordPress transient to store the value of $myvar is the best solution in my case. One reason is because in my case, the browser does not need to know the value of the variable.

Here is some more information to make an informed decision in your specific use case:

  • This separate Stack Exchange question has an answer which provides more information on all the ways to persist data in WordPress, including the use of transients.
  • This link has more info on specific options.
  • PHP sessions don't seem to be suggested for WordPress uses (see the previous two links). But this link has more info on them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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