the user can register on the frontend form, and will be auto logged in after submission. Then he should be redirected to a custom URL, but the redirect is not working.

// Shortcode für das Registrierungsformular
function custom_registration_form_shortcode() {
    ob_start(); // Pufferung starten

    // Überprüfen, ob das Formular abgesendet wurde
    if (isset($_POST['action']) && $_POST['action'] === 'custom_user_registration') {
        $username = sanitize_user($_POST['username']);
        $email = sanitize_email($_POST['email']);
        $password = $_POST['password'];

        // Überprüfe die Eindeutigkeit des Benutzernamens und der E-Mail-Adresse
        if (username_exists($username) || email_exists($email)) {
            echo '<p class="error-message">Benutzername oder E-Mail-Adresse existieren bereits.</p>';
        } else {
            // Hashen des Passworts, bevor es in die Datenbank eingefügt wird
            $hashed_password = wp_hash_password($password);

            $userdata = array(
                'user_login' => $username,
                'user_email' => $email,
                'user_pass'  => $hashed_password,

            $user_id = wp_insert_user($userdata);

            if (!is_wp_error($user_id)) {
                // Erfolgreiche Registrierung
                //echo '<p class="success-message">Registrierung erfolgreich!</p>';
                $_POST = array(); // Formulardaten zurücksetzen
                echo '<pre>';
                var_dump( $_POST );
                echo '</pre>';

                // Benutzer automatisch einloggen
                $user = get_user_by('id', $user_id);
                do_action('wp_login', $user->user_login, $user);            

                // Weiterleitung zur Autorenseite des Benutzers
                wp_redirect( home_url(). '/redirect/' );
            } else {
                // Fehler bei der Registrierung
                echo '<p class="error-message">Fehler bei der Registrierung: ' . $user_id->get_error_message() . '</p>';

    // HTML-Formular ausgeben
    <form method="post">
        <input type="hidden" name="action" value="custom_user_registration">
        <label for="username">Benutzername:</label>
        <input type="text" name="username" id="username" required value="<?php echo isset($_POST['username']) ? esc_attr($_POST['username']) : ''; ?>">

        <label for="email">E-Mail:</label>
        <input type="email" name="email" id="email" required value="<?php echo isset($_POST['email']) ? esc_attr($_POST['email']) : ''; ?>">

        <label for="password">Passwort:</label>
        <input type="password" name="password" id="password" required>

        <input type="submit" value="Registrieren">

    return ob_get_clean(); // Rückgabe des gepufferten HTML-Inhalts
add_shortcode('registration_form', 'custom_registration_form_shortcode');

1 Answer 1


I was able to get your code to work with the help of the answers to this question. I think you only need wp_set_current_user and wp_set_auth_cookie for the login functionality. Here is my revision of your innermost nested conditional, the rest is the same:

if (!is_wp_error($user_id)) {
  $url = home_url() . '/redirect';
  wp_redirect( $url );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.