I've written a custom plugin. When page-a on my site is loaded:
- 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
). - 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.