WordPress Redirect Not Working – AJAX Callback Response Not Picked Up

Since this is an ajax call, you will have to make javascript handle the redirect.

Your code is currently getting executed for the ajax response which is probably working just fine.

You have to think of the ajax request as the browser making a second request behind the scenes of your page. Since the page has finished rendering, nothing will change on the page unless the user does something (like click a button) or javascript does something (like respond to an ajax request).

In your php response, use something like this:

<?php
wp_send_json_error( array( 'message' => 'Error message', 'redirect_url' => home_url() ) );

In your ajax request, use something like this:

var ajaxPost = jQuery.post(
  game_mode_register.ajax_url, {
    action: 'game-mode-register',
    type: jQuery(this).attr('value'),
    crossDomain: true
  }, function(response) {
    console.log('success');
  });

ajaxPost.fail(function(response) {
  console.log('fail response: ' + response);
  //window.location.href = response.redirect_url
});

UPDATE: code above to match updated question. Also updated to use older core jQuery version.

Keep in mind jQuery.post() is the same as jQuery.ajax({ method: 'POST'});

In the case above, you’re throwing an error that your ajax call will pick up on. Then the ajax function for a failed request fires. This is where you handle the error and redirect the user.