1

I am trying to login a user from custom user registration form. My Code for Custom user Register is

function register_user($data) {
        $userdata = array(
            'user_login' => $data['email'],
            'user_email' => $data['email'],
            'user_pass' => $data['pssword'],
            'user_nicename' => $data['fname'],
            'display_name' => $data['fname'],
        );
        $usermetedata = array(
            'son_name' => $data['son_name'],
            'shape' => $data['shape'],
            'siret' => $data['siret'],
            'company_email' => $data['company_email'],
            'company_activity' => $data['activity'],
            'staff' => $data['staff'],
            'position_held' => $data['position_held'],
            'company_phone' => $data['company_phone'],
            'company_mobile' => $data['company_mobile'],
            'company_address' => $data['company_address'],
            'company_postal' => $data['company_postal'],
            'company_ville' => $data['company_ville'],
            'url' => $data['url'],
            'personal_region' => $data['personal_region'],
            'personal_address' => $data['personal_address'],
            'personal_ville' => $data['personal_ville'],
            'personal_postal' => $data['personal_postal'],
            'personal_phone' => $data['personal_phone'],
            'personal_mobile' => $data['personal_mobile'],
            'dob' => $data['dob'],
            'invite_email' => $data['invite_email']
        );
        $userid = wp_insert_user($userdata);
        foreach ($usermetedata as $key => $val):
            add_user_meta($userid, $key, $val);
        endforeach;
        gdlr_new_user_registered($userid);
       // echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-admin">login page</a>.';
        if ($userid) {
            user_activation($userid, $data);

        }

    }

And My code for Login currently register user is

function gdlr_new_user_registered( $user_id ) {
    $user = get_user_by( 'id', $user_id ); 
    wp_set_auth_cookie( $user_id, false, is_ssl() );
    $redirect_to = site_url().'le-reseau-social/teepy-tableau-de-bord/';

    do_action( 'wp_login', $user->user_login );
    return $redirect_to;

}
 add_filter('login_redirect', 'gdlr_new_user_registered', 10, 3); 

But I am not able to login please suggest me or let me know if i am doing some mistake.

Thanks

3 Answers 3

0

For login functionality WordPress provide a function wp_login_form(), It will generate the html for you. Simply change CSS.

For more information see it in WordPress Codex

There is no simple way to register user in wordpress. Follow the bellow steps.

  1. Login into WordPress admin -> Go to settings -> General Settings -> Check Membership Anyone can register -> Select New User Default Role -> Save.
  2. show the frontend form - Create a page template and show the form their.
  3. save the data on submission - Create another page template to handle frontend form submission. You can also use the same page. While inserting data please take care because you have to insert data in 2 tables, 1 is user and another is user_meta. See wordpress table structure.

I am keeping this answer short and sharing couple of url that will help you in coding.

How to display WordPress User Registration Form in front end of the website?

1
  • There is actually api to register users and log them in. Using the default forms or settings is totally not a requirement Commented Nov 13, 2015 at 12:25
0

Can you try to change in you function as

   if ( $user && ! user_can( $user->ID, 'manage_options' ) ) {
    wp_set_current_user( $user->ID, $user_login );
    wp_set_auth_cookie( $user->ID );
    do_action( 'wp_login', $user_login );
    wp_redirect( home_url() );
    exit()
0

I think you have to use this after registration for autologin.

 if ($userid) {
$creds = array(
        'user_login'    => $data['email'],
        'user_password' => $data['pssword'],
        'remember'      => true
    );

    $user = wp_signon( $creds, false );
}

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.