AJAX request on the frontend always returns 0 if user is not admin

As mmm didn’t post an answer but just wrote a comment, here is the answer:

As the documentation for wp_ajax states in its notes, the hook only fires for logged in users.
If you want to use an ajax call on the frontend for users that are not logged in, you have to use the wp_ajax_nopriv hook.

So instead of

add_action( 'wp_ajax_save_like', 'lr_save_like_request' );

you have to write

add_action( 'wp_ajax_nopriv_save_like', 'lr_save_like_request' );

Thats all, the functionality of the two hooks is exactly the same, the only difference is that one fires only for logged in users and the other one for everybody.

Leave a Comment