如何在邮件中设置默认的wordpress身份验证URL

时间:2018-08-01 09:16:42

标签: wordpress

1)首先创建用户

$default_newuser = array(
                                                'user_pass' =>  $cpassworddata,
                                                'user_login' => $username,
                                                'user_email' => $emaildata,
                                                'user_nicename' => $firstname.$lastname,
                                                'display_name'  => $firstname,
                                                'nickname'      => $firstname,
                                                'first_name' => $firstname,
                                                'last_name' => $lastname,
                                                'role' => 'subscriber'
                                    );
                $user_id = wp_insert_user($default_newuser);

2)生成密钥代码

$user_data = get_user_by( 'login', $username );
                if ( $user_id && !is_wp_error( $user_id ) ) {
                    //$code = sha1( $user_id . time() );
                    //$key        = get_password_reset_key( $user_data );
                    $request_data = '';
                    $action_name = 'confirmaction';
                    $request_id = wp_insert_post( array(
                                                        'post_author'   => $user_id,
                                                        'post_name'     => $action_name,
                                                        'post_title'    => $emaildata,
                                                        'post_content'  => wp_json_encode( $request_data ),
                                                        'post_status'   => 'request-pending',
                                                        'post_type'     => 'user_request',
                                                        'post_date'     => current_time( 'mysql', false ),
                                                        'post_date_gmt' => current_time( 'mysql', true ),
                                                    ), true );

                    //$key = wp_generate_password( 20, false );
                    $key = wp_generate_user_request_key( $request_id );
                    do_action( 'retrieve_password_key', $user_data->user_login, $key );
                    if ( empty( $wp_hasher ) ) {
                            require_once ABSPATH . WPINC . '/class-phpass.php';
                            $wp_hasher = new PasswordHash( 8, true );
                    }
                    $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
                    $user_login = $user_data->user_login;
                    $wpdb->update( 
                                    'wp_users', 
                                    array( 
                                                'user_activation_key' => $hashed 
                                    ), 
                                    array( 'ID' => $user_id ), 
                                    array( 
                                            '%s'    // value1
                                    ), 
                                    array( '%d' ) 
                        );
                    //$user_id = wp_update_user( array( 'ID' => $user_id, 'user_activation_key' => $code ) );
                    //$activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink());
                    //$activation_linl =  wp_login_url() .'?action=rp&key='.$code.'&login='.$username;
                    //$activation_linl = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' );
                    $activation_linl  = add_query_arg( array('action'=> 'confirmaction','request_id'=> $request_id,'confirm_key' => $key,
        ), site_url( 'wp-login.php' ) );

3)发送了一个邮件代码

$to = $emaildata;   
                    $subject = 'User Registration form';
                    $headers = "From: " . strip_tags('test@test.com') . "\r\n";
                    $headers .= "Reply-To: ". strip_tags('test@test.com') . "\r\n";
                    $headers .= "MIME-Version: 1.0\r\n";
                    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
                    $message = '<html><body>';
                    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
                    $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $username . "</td></tr>";
                    $message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($emaildata) . "</td></tr>";
                    $message .= "<tr><td><strong>First Name:</strong> </td><td>" . strip_tags($firstname) . "</td></tr>";
                    $message .= "<tr><td><strong>Last Name:</strong> </td><td>" . strip_tags($lastname) . "</td></tr>";
                    $message .= "<tr><td><strong>Activation Link:</strong> </td><td><a href='" . $activation_linl . "'>Activation Link</a></td></tr>";
                    $message .= "</table>";
                    $message .= "</body></html>";   
                    wp_mail($to, $subject, $message, $headers);

0 个答案:

没有答案