-1

我正在注册一个新用户并使用do_action( 'wp_login', $user_login ). 在我最近更新 WordPress 之前一切正常。我检查了说明wp-login已贬值的法典wp_signon()。代码如下:


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 );
            }
        }
    }
}

用户已创建但未登录。

4

1 回答 1

0

您将 $username 传递给 wp_set_current_user(),但看起来您没有在任何地方定义它。无论如何,这个论点是可选的——我建议你把它关掉。

您还在 $userdata 和 $current_user 上执行 print_r。我认为它们也没有设置。不要假设您的代码将在全局范围内运行——在您的代码中显式地使 $current_user 成为全局,或者(更好地)处理从 wp_set_current_user() 返回的对象。

于 2013-01-18T05:31:10.707 回答