Custom signup redirection to external URL not working
-
Hello,
I am using User Registration plugin to register my users. I have created a widget to show a modal with Signup and Login form. I have one more API where the user gets created from this same form. So I have created custom code to handle Login and Signup. After successful Login or Signup, I want my users to redirect to a certain URL. For Login that is working fine, but it is not working for Signup. If anyone knows the issue, please help. Thank you in advance. Here I have added my code for Signup:
<?php function custom_pre_user_registration($form_data) { // Extract necessary data from form submission $email = isset($form_data['user_email']->value) ? sanitize_email($form_data['user_email']->value) : ''; $password = isset($form_data['user_pass']->value) ? sanitize_text_field($form_data['user_pass']->value) : ''; $target_profession_id = isset($form_data['targetProfessionId']->value) ? sanitize_text_field($form_data['targetProfessionId']->value) : ''; // Your API endpoint $api_url = SERVER_URL . '/api/v1/auth/signup'; // Data to be sent to the API $data = array( 'email' => $email, 'password' => $password, 'targetProfessionId' => $target_profession_id, ); // Set up the request $args = array( 'body' => json_encode($data), 'headers' => array( 'Content-Type' => 'application/json', ), ); // Make the API call $response = wp_remote_post($api_url, $args); // Check for API errors if (is_wp_error($response)) { $error_message = $response->get_error_message(); error_log("API Error: $error_message"); wp_send_json_error(array('message' => $error_message)); } $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); // Log the full response body for debugging error_log("API Response: " . print_r($response_body, true)); // Check the headers for the Set-Cookie header $headers = wp_remote_retrieve_headers($response); error_log("API Response Headers: " . print_r($headers, true)); // Manually set the cookie received from the API response if (isset($headers['set-cookie'])) { $set_cookie_header = $headers['set-cookie']; if (is_array($set_cookie_header)) { foreach ($set_cookie_header as $cookie) { header("Set-Cookie: $cookie", false); error_log("Setting cookie: $cookie"); } } else { header("Set-Cookie: $set_cookie_header", false); error_log("Setting cookie: $set_cookie_header"); } } // Check the API response for success if (isset($response_data['success']) && $response_data['success'] === true) { // Get the UUID from the API response $userName = isset($response_data['data']['userName']) ? sanitize_text_field($response_data['data']['userName']) : ''; $uuid = isset($response_data['data']['id']) ? sanitize_text_field($response_data['data']['id']) : ''; // Create the WordPress user with the same UUID $user_id = wp_create_user($userName, $password, $email); // Check if the user was created successfully if (is_wp_error($user_id)) { $error_message = $user_id->get_error_message(); error_log("WordPress User Creation Error: $error_message"); wp_send_json_error(array('message' => $error_message)); } // Store the UUID in user meta update_user_meta($user_id, 'uuid', $uuid); // Log the user in automatically wp_set_current_user($user_id); wp_set_auth_cookie($user_id); do_action('wp_login', $userName, get_userdata($user_id)); error_log("API Registration and WordPress User Creation Success"); // Check if the request is an AJAX request if (wp_doing_ajax()) { wp_send_json_success(array('redirect_url' => APP_LANDING . '/' . $response_data['data']['id'])); } else { wp_redirect(APP_LANDING . '/' . $response_data['data']['id']); exit; } } else { // Handle API registration failure $error_message = isset($response_data['message']) ? $response_data['message'] : __('Registration failed.', 'textdomain'); error_log("API Registration Failure: $error_message"); if (wp_doing_ajax()) { wp_send_json_error(array('message' => $error_message)); } else { wp_die(__('API Error: ', 'textdomain') . $error_message); } } } add_action('user_registration_before_register_user_action', 'custom_pre_user_registration', 10, 1); -
It looks like you’ve got a solid setup for handling user registration via an API in WordPress, but the redirect after successful signup isn’t working as expected. Here are a few things you can check and modify to troubleshoot and potentially fix the issue:Steps to Troubleshoot the Redirect Issue
- Check AJAX Request Handling:
- Ensure that your signup form is correctly configured to handle AJAX submissions. If the form is not set up for AJAX, the
wp_doing_ajax()check may not return true, and the redirect will attempt to occur viawp_redirect, which might not be executed properly in an AJAX context.
- Ensure that your signup form is correctly configured to handle AJAX submissions. If the form is not set up for AJAX, the
- Verify
APP_LANDING:- Make sure that
APP_LANDINGis correctly defined and points to a valid URL. If this constant is not set or is incorrect, the redirect URL could be invalid.
- Make sure that
- Debugging Redirect Logic:
- Add debugging logs before the
wp_redirectcall to confirm that the execution reaches that point and to see what URL it attempts to redirect to.
- Add debugging logs before the
- Check AJAX Request Handling:
-
Thank you for your response.
I have added debug log before redirect and wp_doing_ajax() is true. So in the if statement, first one is logging there. And the APP_LANDING is correct as I am doing similar thing in login.php and that is working fine. Only difference with the login is, I am using add_filter in login and add_action in signup.
- The topic ‘Custom signup redirection to external URL not working’ is closed to new replies.