How to fix WordPress admin-ajax.php 302 Redirection error

Needed to post this to help any other poor soul trying to get WordPress to provide an AJAX service to non-logged in users. Note this is only necessary if you’ve got a plugin active that bans front-end users from back-end functionality. Most of the front-end login plugins do this.

In addition to registering the call with the admin-ajax action…

add_action( 'wp_ajax_AJAXfunctionCall', 'JavascriptFunctionName' );
add_action( 'wp_ajax_nopriv_AJAXfunctionCall', 'JavascriptFunctionName');	

…you also need to add code like this to specifically call the action for users that aren’t logged in:

if(isset($_REQUEST['action']) && $_REQUEST['action']=='AJAXfunctionCall'):
        do_action( 'wp_ajax_' . $_REQUEST['action'] );
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );

Leave a Reply




Similar Articles

Bespoke CMS == Vendor Lock In?

Bespoke content management systems are nearly always just vendor lock-in. There, I said it.

Hopefully Google will index this and some hapless business owner will avoid commissioning a web agency to implement their website using a proprietary CMS.