DEV Community

Nazmul Alom
Nazmul Alom

Posted on

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

}
add_action('user_registration_before_register_user_action', 'custom_pre_user_registration', 10, 1);`

Top comments (0)