simple wordpress ajax plugin not working when not logged in

The question:

anyone encountered this bug that it will work when you are logged in but once you log out it won’t work anymore.

this is a very simple plugin that will fetch zip code and display the corresponding rate.

here’s the code.


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You want to use wp_ajax_nopriv_ for handling ajax for non-authenticated users:

// logged-out users
add_action( 'wp_ajax_nopriv_my_action_search_key_press', 'action_search_key_press_callback' );

// authenticated users
add_action( 'wp_ajax_my_action_search_key_press', 'action_search_key_press_callback' );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment