I’m using ajax to post the form data. In the end I want to redirect to homepage. I’m trying following code, it does not work. It returns error 302.
How can I redirect in the following function? Thanks.
add_action('wp_ajax_nopriv_custom_register', 'custom_register');
add_action('wp_ajax_custom_register', 'custom_register');
function custom_register(){
//process
wp_redirect( home_url() );
exit;
}
The AJAX request runs in the background. Redirects here do not affect the main page. And 302 is not an error, it is just a status code.
Your AJAX response should return either the URL and the status code to the calling page or just a number like 1. Then you handle the redirect in the calling page:
It’s some years behind but I think this answer can help others:
I have used a function to redirect from a JS AJAX call. First, it sets some values to database and then tries to redirect in this way:
But it just returns same page followed by ‘undefined’.
I finally solved directly the redirection part in JS:
Works also :
If you want to redirect then use javascript
window.lcation="your url"
in your php code.Try this in your function that is called by the ajax call;