Delmontee
Delmontee

Reputation: 2364

Wordpress get_ajax.php returns 0 in ALL except chrome

I created a test Wordpress function that returns the text "test" when the function is called via the get_ajax.php in the url, ie.

http://[somesitename.com]/wp-admin/admin-ajax.php?action=get_test

The test function is simply:

add_action( 'wp_ajax_get_test', 'gmsl_test' );
function gmsl_test(){
    echo "TEST";
    exit();
}

This returns "TEST" in Chrome, but 0 in ie11, edge and firefox. Does anybody know why that might be? Screenshot can be seen at https://postimg.org/image/wckvqg635/

Upvotes: 0

Views: 38

Answers (1)

Delmontee
Delmontee

Reputation: 2364

I have found the answer - I was looking at it from the wrong angle. It wasn't a browser issue. Chrome was the only one of them that had a logged in user! So the problem was the ajax query returns with 0 if the user isn't logged in!!

The solution - if you want wordpress ajax to work for none-logged-in users you need to include a second action (nopriv):

add_action( 'wp_ajax_get_test', 'gmsl_test' );
add_action( 'wp_ajax_nopriv_get_test', 'gmsl_test' );

(There's 4 hours I'll never get back)

Upvotes: 0

Related Questions