在Wordpress中通过API创建新用户时如何发送电子邮件密码? [英] How to send email password when creating a new user by API in Wordpress?

查看:45
本文介绍了在Wordpress中通过API创建新用户时如何发送电子邮件密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用以下行通过 API 创建新用户:

It is possible to create a new user by API with the following line:

$user_id = wp_insert_user( $user_data );

我想知道如何向新创建的用户发送一封包含他的密码的电子邮件?Wordpress API 中是否有任何函数可以处理这项工作,还是我应该自己创建和发送电子邮件?

I wonder how to send the newly created user an email that contains his password? Is there any function in Wordpress API that handles this job or should I create and send an email by myself?

推荐答案

我假设您正在生成密码并将其添加到 $user_data 数组?

I assume you are generating the password and adding it to the $user_data array?

如果没有,你可以用这个来生成密码-

If not, you can use this to generate a password -

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

虽然可能有一种方法可以连接到通用的 WP 发送密码电子邮件,但我只是使用我自己的.这样,我就可以自定义内容,并使其看起来像我网站上的其他电子邮件.

And while there probably is a way of hooking in to the generic WP send password email, I just use my own. That way, I can customise the content, and make it look like other emails from my site.

请注意,我已经设置了一个用于注册的类,因此如果您还没有,则需要删除 $this-> 的实例.

Note that I have set up a Class for registration, so if you have not, you will need to remove instances of $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');

    }

这篇关于在Wordpress中通过API创建新用户时如何发送电子邮件密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆