There are two issues
Broken Callbacks
You’ve used:
add_action('wp_ajax_nopriv_my_php_ajax_function','my_php_ajax_function' );
This is equivalent to:
When `wp_ajax_nopriv_my_php_ajax_function` happens
do: `my_php_ajax_function()`
Which is not what you wanted, because that is not a function, and there is no my_php_ajax_function
function.
You also don’t just want it on that class, you want it on that specific instance of that class, afterall how will it know what $this
is?
add_action
expects parameter 2 to be a callable, so lets look up the callable that matches a class function:
https://www.php.net/manual/en/language.types.callable.php
// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
So instead of "function name"
you need to pass [ $this, "class function name" ]
Sharing Data Across Requests
Unlike webapps built with Java and other languages, PHP apps are loaded from scratch each time a request is handled and have a limited lifespan. There is no shared persistent program running in the background.
Everytime your browser talks to your server, be it to load a page, or via javascript, WordPress is loaded from a blank slate. When WordPress has responded, it is destroyed and nothing is preserved.
This means all variables are wiped at the end of every request.
If you want to persist/save those values, you have several options:
- store it in a cookie
- store it in user meta if the user is logged in
- store it in the browser and send it with every request
- store it on a post or page ( best for data specific to a post that affects all users )
- store it in an option or transient ( best for global site level data that affects all users )