簡體   English   中英

在Wordpress中通過API創建新用戶時如何發送電子郵件密碼?

[英]How to send email password when creating a new user by API in Wordpress?

可以使用以下行通過 API 創建新用戶:

$user_id = wp_insert_user( $user_data );

我想知道如何向新創建的用戶發送一封包含他的密碼的電子郵件? Wordpress API 中是否有任何函數可以處理這項工作,還是我應該自己創建和發送電子郵件?

正如大衛猜測的(但沒有指定),Wordpress 中有一些功能可以做到這一點: wp_new_user_notification($user_id, $user_pass)

所以,重寫上面的代碼,它應該是這樣的(代碼在 4.3.1 參數棄用后被編輯):

$user_id = wp_insert_user( $user_data );
wp_new_user_notification( $user_id, null, 'both' );

編輯:另請參閱下面@ale 的評論。

我假設您正在生成密碼並將其添加到$user_data數組中?

如果沒有,您可以使用它來生成密碼 -

$this->password = wp_generate_password(6, false);
$user_data['user_pass'] = $this->password;

雖然可能有一種方法可以連接到通用的 WP 發送密碼電子郵件,但我只是使用我自己的。 這樣,我可以自定義內容,並使其看起來像我網站上的其他電子郵件。

請注意,我已經設置了一個用於注冊的類,因此如果您還沒有,則需要刪除$this->實例。

function prepare_email(){

        $confirmation_to = $_REQUEST['email_address'];
        $confirmation_subject = 'Confirmation - Registration to My Site';
        $confirmation_message = 'Hi '.$_REQUEST['first_name'].',<br /></br />Thank you for registering with My Site. Your account has been set up and you can log in using the following details -<br /><br />'
            .'<strong>Username:</strong> '.$_REQUEST['username']
            .'<br /><strong>Password:</strong> '.$this->password
            .'<br /><br />Once you have logged in, please ensure that you visit the Site Admin and change you password so that you don\'t forget it in the future.';
        $headers = 'MIME-Version: 1.0'."\r\n";
        $headers.= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
        $confirmation_headers = $headers.'From: My Site <no-reply@mysite.com>'."\r\n";

        $this->form_for_email = compact('confirmation_to', 'confirmation_subject', 'confirmation_message', 'confirmation_headers');

    }

已接受的答案現已過時。 使用wp_new_user_notification()的答案有效,並將發送類似 WordPress 的郵件。

不過,您可能希望按照 David Gard 的建議發送自己的郵件。 這是執行此操作的一些代碼。 您可以將其用作函數或類中的方法。

/**
 * Send mail with a reset password link
 *
 * @param  int $user_id  User ID
 */
function notify_new_user( $user_id )
{
    $user = get_userdata( $user_id );

    $subject   = '[website] Connection details';
    $mail_from = get_bloginfo('admin_email');
    $mail_to   = $user->user_email;
    $headers   = array(
        'Content-Type: text/html; charset=UTF-8',
        'From: ' . $mail_from,
    );

    $key = get_password_reset_key( $user );
    if ( is_wp_error( $key ) ) {
        return;
    }
    $url_password = network_site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user->user_login ) );

    $body = '<p>HTML body in your own style.</p>';
    $body .= '<p>It must include the login identifier: ' . $user->user_login . '</p>';
    $body .= '<p>And the link: ' . $url_password . '</p>';
    
    wp_mail( $mail_to, $subject, $body, $headers );
}

如果不想在函數中包含 HTML 內容,可以使用模板引擎。 這是您可以添加到上述代碼的示例。

// Set up Mustache
$path = get_stylesheet_directory() . '/mails';
$dir  = wp_get_upload_dir();
$m    = new \Mustache_Engine( [
    'cache'           => $dir['basedir'] . '/mustache_cache',
    'loader'          => new \Mustache_Loader_FilesystemLoader( $path ),
    'partials_loader' => new \Mustache_Loader_FilesystemLoader( $path . '/layouts' ),
] );

// Variable data in the mail
$data['mail']['title'] = $subject;          // {{mail.title}}
$data['identifier']    = $user->user_login; // {{identifier}}
$data['url-password']  = $url_password;     // {{url-password}}

$body = $m->render( 'mail-template-new-user', $data );

小胡子標簽文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM