1

I cannot get a redirect working in my custom wordpress plugin. I have a button on a page which triggers an AJAX action which calls a function.

In my function (which is definitely being called as I can see the output "User is not logged in" in my debug log), I have:

error_log("User is not logged in!");
wp_redirect(home_url());
exit();

Things I've tried: Hardcoding the url, Trying other urls, Using exit(); and exit, Using header()

Im not sure why this is not working. Every other similar question has answers saying add exit(); after but that has not worked for me. Thanks.

Edit:

Adapted AJAX callback as follows:

<script>

// Create a function to pick up the link click
jQuery(document).ready(function($) {
    $('.button').click(function(e){
        e.preventDefault();
        console.log("Hi")
        jQuery.post(
            game_mode_register.ajax_url, 
            {
                'action': 'game-mode-register',
                'type':   $(this).attr("value")
             }.fail(function(response){
                  console.log("ajax response");
                  //window.location.href = response.redirect_url
             });
        );
    });
 });

Error output: SyntaxError: missing ) after argument list, refering to the line right after the //window.location...

Also, it seems like my wp_redirect is getting picked up but blocked. The Javascript console says:

Blocked loading mixed active content “http://my_website.com/

I have an SSL certificate that is up to date. Not sure what this means either.

Edit:

Going from direction given by first answer, failed attempts include:

wp_send_json_success( $data );
wp_send_json_error( $data );
wp_send_json( $data );
echo json_encode($data);

1 Answer 1

1

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.

7
  • Ok, I think this is definitely on the right track. My ajax callback uses wordpress ajax hooks though so this format doesn't work exactly. I edited the question with my format with the addition of your code and the associated error in the Javascript console. Also, I missed this before but using wp_redirect actually did something in the Javascript console too, I'll edit that in. Apr 3, 2019 at 3:30
  • Updated my response. Also note the crossDomain: true for your issues with http/https. Although you should definitely change your ajax_url to use https. Apr 3, 2019 at 13:36
  • Hm, this code doesn't seem to pickup the error. It just runs through the first part and logs 'success'. It doesn't run through the .fail portion. I also tried the exact code used here: codex.wordpress.org/Function_Reference/wp_send_json, and put it in the first part where it currently logs 'success' and it didn't work. Apr 4, 2019 at 0:09
  • Ya the https stuff is a whole other can of worms from what I've researched about it. I'll deal with that later I just want this redirect stuff to work for now, it is frustrating. Apr 4, 2019 at 0:10
  • If you are using an older version of jQuery, the fail function won't fire. Try checking for an error in your success function, eg console.log(response); Apr 4, 2019 at 1:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.