I'm trying to store data from a shortcode attribute in a class property, so I can use it later in other functions. But somehow I'm always ending up with the initial value of the property. Here's a reduced example to showcase my problem:

While my_var is correctly set to "yeeha!" in the shortcode function, in the ajax_handler() function it shows as null. Is this an issue of scope? I thought it might be because both the shortcode and the ajax handler functions are called from outside the object context and thus $this doesn't point to test_obj, but other questions and articles I've read didn't seem to have this problem. So.. what is my issue here?

I thought about doing that, but it seemed like an overcomplicated solution to me, since the JS doesn't need to know my_var at all and would simply pass it back through AJAX. But maybe that's the way to go. Can you explain to me why calling the AJAX handler is a "different request", as you said? Since the AJAX handler isn't static, I thought it would be called in the same object context as the shortcode handler.
– Sven ChmielewskiJul 8 '17 at 13:51

1

@SvenChmielewski after the page that contains your shortcode is sent to the browser from your server, the connection is closed and that request is gone from memory. Your ajax request is then sent from that page, but it's an entirely new request, like opening two different browser windows and visiting two different URLs, there is no state maintained between the two.
– MiloJul 8 '17 at 16:02