0

I have setup some process that should register a new user and login the new user right after registration. This is my function for registration:

    add_filter('the_content', 'registeration_submit_user');

function registeration_submit_user($content) {
    // Get the current post
    global $post;
    // Get the value of the slug
    $page_slug = $post->post_name;
    // Checks is the current page is the custom registration page and it has a $_POST value
    if ($page_slug === "registration" && $_POST['nons_ipt'] === 'registeration_submit_user') {
        // Hendle the registration
        $user_email = 'yos@sjdj.com';
        $arr = explode("@", $user_email, 2);
        $user_name = $arr[0];
        $user_id = username_exists($user_name);
        if (!$user_id and email_exists($user_email) == false) {
            $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
            $user_id = wp_create_user($user_name, $random_password, $user_email);
        } else {
            $random_password = __('User already exists.  Password inherited.' . $response);
        }
        return $random_password;
    } else {
        // Rest of pages
        return $content;
    }
}

Now, I would like to login the user right after wp_create_user function. I tried to do this with the following code:

$creds = array();
$creds['user_login'] = $user_name;
$creds['user_password'] = $random_password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
  echo $user->get_error_message();

But the problem is that i get this message:

Warning: Cannot modify header information - headers already sent by (output started at...

What is the best way to make this happen?

  • The error tells you the problem- you have to log someone in before any output is sent to the browser. – Milo Mar 14 '15 at 14:25
  • That is why i am asking, because i would like to login right after registration. Maybe I can redirect to some page? but even then, how i can implement login in custom page? it should be in the init and i do not know the page id in the init stage. – Dori Mar 14 '15 at 14:35
  • what hooks are you using?... can you complete the code up to hooks and related functions please. – Reigel Mar 14 '15 at 15:15
  • I have update the code above – Dori Mar 14 '15 at 15:31
  • 2
    if your POST fields are sufficiently unique, I don't see why you have to know it's the registration page to process a registration, anywhere between init and template_redirect would work. – Milo Mar 14 '15 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.