0

I am trying to create a custom user and then log them in programatically, whereafter I want to redirect them to a payment page.

I have the following code in my theme's functions.php file:

function sp_registration_process_hook() {

    if (isset($_POST['adduser']) && isset($_POST['add-nonce']) && wp_verify_nonce($_POST['add-nonce'], 'add-user')) {
        // die if the nonce fails
        if ( !wp_verify_nonce($_POST['add-nonce'],'add-user') ) {
            wp_die('Sorry! That was secure, guess you\'re cheatin huh!');
        } else {

            //create a new role
            remove_role( 'service_provider' );
            $result = add_role(
                'service_provider',
                __( 'Service Provider' ),
                array(
                    'read'         => true,  // true allows this capability
                    'edit_posts'   => true,
                    'delete_posts' => true, // Use false to explicitly deny
                    'level_0'      => true,
                    'level_1'      => true
                )
            );

            // auto generate a password
            $user_pass = wp_generate_password();

            echo $user_pass;

            $user_login = esc_attr( $_POST['user_name'] );
            $user_email = esc_attr( $_POST['email'] );

            // setup some error checks
            if ( !$user_login ) {
                $error = 'A username is required for registration.';
            }
            elseif ( username_exists($user_login) ) {
                $error = 'Sorry, that username already exists!';
            }
            elseif ( !is_email($user_email, true) ) {
                $error = 'You must enter a valid email address.';
            }
            elseif ( email_exists($user_email) ) {
                $error = 'Sorry, that email address is already used!';
            }
            // setup new users and send notification
            else
            {
                $user_id = wp_create_user($user_email, $user_pass, $user_email);
                wp_update_user(
                    array(
                        'ID' => $user_id,
                        'nickname' => $user_email
                    )
                );
                $user = new WP_User($user_id);
                $user->set_role('service_provider');

                custom_login( $user_email, $user_pass );
            }
        }
    }
}
add_action('process_sp_registration_form', 'sp_registration_process_hook');

function custom_login($email, $pass) {
    $creds = array();
    $creds['user_login'] = $email;
    $creds['user_password'] = $pass;
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) )
        echo $user->get_error_message();
}
// run it before the headers and cookies are sent
add_action( 'after_setup_theme', 'custom_login' );

The user gets created fine, but it does not log in the user automatically. When I look at the front end, the user is definitely not logged in after the post.

Can you see anything obvious that I am doing wrong? Any help will be appreciated.

Jacques

1 Answer 1

0

I have similar functionality on one of my app. I used wp_set_auth_cookie().

Remove the custom_login() function and use wp_set_auth_cookie($user_id) in its place.

2
  • Hi Sisir, tried that, makes no difference - user is created but not logged in.
    – Jacques
    Jul 26, 2015 at 12:50
  • Can you try hooking into template_redirect instead of process_sp_registration_form? The code must be run before any output to browser.
    – Sisir
    Jul 26, 2015 at 13:00

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.