<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nazmul Alom</title>
    <description>The latest articles on DEV Community by Nazmul Alom (@nazmul_alom_dfd38a4363246).</description>
    <link>https://dev.to/nazmul_alom_dfd38a4363246</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1773268%2F0088f3d2-21b2-43d5-921a-4dab339c1ef3.jpg</url>
      <title>DEV Community: Nazmul Alom</title>
      <link>https://dev.to/nazmul_alom_dfd38a4363246</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nazmul_alom_dfd38a4363246"/>
    <language>en</language>
    <item>
      <title>Custom signup redirection to external URL not working</title>
      <dc:creator>Nazmul Alom</dc:creator>
      <pubDate>Fri, 12 Jul 2024 17:52:48 +0000</pubDate>
      <link>https://dev.to/nazmul_alom_dfd38a4363246/custom-signup-redirection-to-external-url-not-working-260c</link>
      <guid>https://dev.to/nazmul_alom_dfd38a4363246/custom-signup-redirection-to-external-url-not-working-260c</guid>
      <description>&lt;p&gt;Hello,&lt;br&gt;
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:&lt;/p&gt;

&lt;p&gt;`&amp;lt;?php&lt;/p&gt;

&lt;p&gt;function custom_pre_user_registration($form_data)&lt;br&gt;
{&lt;br&gt;
    // Extract necessary data from form submission&lt;br&gt;
    $email = isset($form_data['user_email']-&amp;gt;value) ? sanitize_email($form_data['user_email']-&amp;gt;value) : '';&lt;br&gt;
    $password = isset($form_data['user_pass']-&amp;gt;value) ? sanitize_text_field($form_data['user_pass']-&amp;gt;value) : '';&lt;br&gt;
    $target_profession_id = isset($form_data['targetProfessionId']-&amp;gt;value) ? sanitize_text_field($form_data['targetProfessionId']-&amp;gt;value) : '';&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Your API endpoint
$api_url = SERVER_URL . '/api/v1/auth/signup';

// Data to be sent to the API
$data = array(
    'email' =&amp;gt; $email,
    'password' =&amp;gt; $password,
    'targetProfessionId' =&amp;gt; $target_profession_id,
);

// Set up the request
$args = array(
    'body' =&amp;gt; json_encode($data),
    'headers' =&amp;gt; array(
        'Content-Type' =&amp;gt; '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-&amp;gt;get_error_message();
    error_log("API Error: $error_message");
    wp_send_json_error(array('message' =&amp;gt; $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']) &amp;amp;&amp;amp; $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-&amp;gt;get_error_message();
        error_log("WordPress User Creation Error: $error_message");
        wp_send_json_error(array('message' =&amp;gt; $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' =&amp;gt; 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' =&amp;gt; $error_message));
    } else {
        wp_die(__('API Error: ', 'textdomain') . $error_message);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
add_action('user_registration_before_register_user_action', 'custom_pre_user_registration', 10, 1);`&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>help</category>
    </item>
  </channel>
</rss>
