0

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:

class ScopeTestClass {
    public $my_var = null;

    function __construct() {
        // The calls to add_action() go here
        // I've omitted them for this example to avoid cluttering up the code
        add_shortcode( 'my_shortcode', array($this, 'my_shortcode_handler'));
    }

    function register_scripts() {
        wp_register_script('some_script', plugin_dir_url(__FILE__) .
            '/js/some_script.js', array('jquery'), '1.0', true);

    public function my_shortcode_handler() {
        $this->my_var = 'yeeha!';
        wp_enqueue_script('some_script');
        wp_localize_script( 'some_script', 'somescript', array(
            'ajax_url' => admin_url( 'admin-ajax.php' )));

    }

    public function ajax_handler() { 
        // This function handles the ajax call from some_script.js.
        // When evaluating $this->my_var here, it is null. 
    }
}

$test_obj = new ScopeTestClass();

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?

0

If you are posting from the script to the AJAX handler, that is an entirely different request, the script has no way of knowing the my_var value, so how can it be passed back to the AJAX.

But this is why you are using localize script isn't it? Why not simply pass the value in that array?

wp_localize_script( 'some_script', 'somescript', array(
        'ajax_url' => admin_url( 'admin-ajax.php' ), 
        'my_var' => $this->my_var 
    )
);
  • 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 Chmielewski Jul 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. – Milo Jul 8 '17 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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