Can an AJAX callback function access a PHP variable which was defined during initial page load?

I've written a custom plugin.When page-a on my site is loaded:My plugin's main PHP prepares data to send to

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).

Thanks.

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).

Thanks.

Share Improve this question asked Feb 10, 2020 at 13:55 cag8fcag8f 1,9973 gold badges21 silver badges31 bronze badges 11
  • 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 Commented 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 Commented 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 Commented 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 Commented 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 Commented Feb 15, 2020 at 10:00
 |  Show 6 more comments

1 Answer 1

Reset to default 0

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744744747a4591231.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信