Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:45:29+00:00 2026-06-17T10:45:29+00:00

I was registering a new user and logging him in using do_action( ‘wp_login’, $user_login

  • 0

I was registering a new user and logging him in using do_action( 'wp_login', $user_login ). It was all working fine until I recently updated WordPress. I checked the Codex which stated that wp-login has been depreciated by wp_signon(). The code is as below:


if ( isset( $_POST[ "submit" ] ) && wp_verify_nonce( $_POST[ 'smart_register_nonce' ], 'smart-register-nonce' ) ) 
{
    $licence_no     = $_POST[ "smart_user_licence" ];           
    $user_login     = $_POST[ "smart_user_login" ]; 
    $user_email     = $_POST[ "smart_user_email" ];
    $user_first     = $_POST[ "smart_user_first" ];
    $user_last      = $_POST[ "smart_user_last" ];
    $user_pass      = $_POST[ "smart_user_pass" ];
    $pass_confirm   = $_POST[ "smart_user_pass_confirm" ];

    // this is required for username checks
    require_once( ABSPATH . WPINC . '/registration.php' );
    // include the wordpress files necessary to run its functions
    include('../classpages/wp-config.php'); // this includes wp-settings.php, which includes wp-db.php, which makes the database connection
    include(ABSPATH . WPINC . '/pluggable-functions.php');

    if(username_exists( $user_login ) ) 
    {
        // Username already registered
        smart_errors()->add( 'username_unavailable', __( 'Username already taken' ) );
    }
    if(!validate_username( $user_login ) ) 
    {
        // invalid username
        smart_errors()->add( 'username_invalid', __( 'Invalid username' ) );
    }
    if( $user_login == '' ) 
    {
        // empty username
        smart_errors()->add( 'username_empty', __( 'Please enter a username' ) );
    }
    if(!is_email( $user_email ) ) 
    {
        //invalid email
        smart_errors()->add( 'email_invalid', __( 'Invalid email' ) );
    }
    if( email_exists( $user_email ) ) 
    {
        //Email address already registered
        smart_errors()->add( 'email_used', __( 'Email already registered' ) );
    }
    if( $user_pass == '' ) 
    {
        // passwords do not match
        smart_errors()->add( 'password_empty', __( 'Please enter a password' ) );
    }
    if( $user_pass != $pass_confirm ) 
    {
        // passwords do not match
        smart_errors()->add( 'password_mismatch', __( 'Passwords do not match' ) );
    }

    $errors = smart_errors()->get_error_messages();

    // only create the user in if there are no errors
    if( empty( $errors ) ) 
    {
        $new_user_id = wp_insert_user(array(
                'user_login'        => $user_login,
                'user_pass'         => $user_pass,
                'user_email'        => $user_email,
                'first_name'        => $user_first,
                'last_name'         => $user_last,
                'user_registered'   => date('Y-m-d H:i:s'),
                'role'              => 'author'
            )
        );

        // Create post object
        $my_post = array(
                  'post_title'    => $user_login,
                  'post_content'  => '',
                  'post_type'     => 'vet-profile',
                  'post_status'   => 'publish',
                  'post_author'   => $new_user_id,
                  'post_category' => array(8,39)
        );

        // Insert the post into the database
        wp_insert_post( $my_post );  

        if( $new_user_id ) 
        {
            // send an email to the admin alerting them of the registration
            wp_new_user_notification( $new_user_id );


            // Make sure user session has started
            $vsessionid = session_id();
            if ( empty( $vsessionid ) ) 
            {
                session_name('PHPSESSID'); 
                session_start();
            }

            // log the new user in
            wp_set_auth_cookie( $new_user_id, true );
            wp_set_current_user( $new_user_id, $user_login);    
            //do_action( 'wp_signon', $user_login );

            $creds = array();
            $creds['user_login'] = $user_login;
            $creds['user_password'] = $user_pass;
            $creds['remember'] = true;

            $user = wp_signon( $creds, false );

            if ( is_wp_error( $user ) ) 
            {
                $error = $user->get_error_message();
            } 
            else 
            {
                wp_set_current_user( $user->ID, $username );
                do_action('set_current_user');

                // send the newly created user to the home page after logging them in
                //$location = "http://example.com";
                //wp_safe_redirect( $location ); 
                //exit;
            }
            if ( $error ) 
            {
                echo $error; 
                print_r( $userdata ); 
                print_r( $current_user );
            }
        }
    }
}

the user is created but it does not login.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:45:31+00:00Added an answer on June 17, 2026 at 10:45 am

    You are passing in $username to wp_set_current_user(), but it doesn’t look like you’ve defined it anywhere. Anyway, that argument is optional — I suggest you leave it off.

    You’re also doing print_r on $userdata and $current_user. I don’t think they are set either. Don’t assume that your code will run in the global scope — explicitly make $current_user global in your code, or (better) work on the object returned from wp_set_current_user().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using AFNetworking registering new users, it all works ok but on the
i have two methodes : one for registering new user AddNewUser(MemberRegisterModel mm) , and
My understanding is that the way to go about registering a new user on
When registering as a new user on our custom DotNetNuke website, if we enter
When registering new user, user can upload photo or leave it as default, but
I was having a problem while registering a new user in Code Igniter and
I am using the built-in register module in asp.net application for registering new users.
I have the following (abridged) DTO for registering a new user: [PropertiesMustMatch(Password, ConfirmPassword, ErrorMessage
As part of registering a new user; we assign them a resource (a Solr
When registering a product, the user can customize the URL of it! As the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.