6

次の行を使用して、APIによって新しいユーザーを作成することができます。

$user_id = wp_insert_user( $user_data );

新しく作成したユーザーにパスワードを含むメールを送信するにはどうすればよいですか?このジョブを処理するWordpressAPIの関数はありますか、それとも自分でメールを作成して送信する必要がありますか?

4

3 に答える 3

31

Davidが推測したように(しかし指定しませんでした)、これを行うための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のコメントも参照してください。

于 2013-02-20T16:08:52.473 に答える
7

パスワードを生成して$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');

    }
于 2012-11-22T09:59:33.820 に答える
0

受け入れられた回答は廃止されました。を使用した回答は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 );

口ひげタグのドキュメント

于 2021-02-13T17:08:15.093 に答える