WordPress

php – Niestandardowe przekierowanie rejestracji do zewnętrznego adresu URL nie działa

  • 12 lipca, 2024
  • 4 min read
php – Niestandardowe przekierowanie rejestracji do zewnętrznego adresu URL nie działa


Używam wtyczki Rejestracja użytkownika, aby rejestrować moich użytkowników. Stworzyłem widżet, aby pokazać okno modalne z formularzem rejestracji i logowania. Mam jeszcze jedno API, w którym użytkownik jest tworzony z tego samego formularza. Więc stworzyłem niestandardowy kod do obsługi logowania i rejestracji. Po pomyślnym zalogowaniu lub rejestracji chcę, aby moi użytkownicy byli przekierowywani do określonego adresu URL. W przypadku logowania działa to dobrze, ale nie działa w przypadku rejestracji. Jeśli ktoś zna problem, proszę o pomoc. Z góry dziękuję. Tutaj dodałem mój kod dla rejestracji:

<?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);


Źródło

Warto przeczytać!  search — jak mogę zastąpić modyfikacje zapytania Kalendarza wydarzeń?