当用户注册REST API时发送电子邮件

时间:2019-05-22 作者:technolaaji

当前正在使用此插件https://wordpress.org/plugins/wp-rest-user/ 还有一个部分,如果你想在用户注册时发送电子邮件(使用wordpress作为无头CMS)

add_action(\'wp_rest_user_user_register\', \'user_registered\');
function user_registered($user) {
    // Do Something
}
已设置WP Mail SMTP插件,以便覆盖WP\\u Mail功能

问题是:如何获得注册用户的电子邮件,以便使用wp\\U邮件功能向他/她发送电子邮件?

1 个回复
最合适的回答,由SO网友:MikeNGarrett 整理而成

您不知道传递给$user 参数,让我们测试一下。

您将希望详细介绍这些条件和响应。这只是您可能想要进行的测试的一个示例。

function user_registered( $user ) {
    // Check to make sure it\'s not an error.
    if ( is_wp_error( $user ) ) {
        return;
    }
    // This is how WordPress checks to make sure the user exists.
    // This could also apply to many other objects, though. 
    if ( ! isset( $user->ID ) ) {
        return; 
    }
    // This checks to make sure you\'re getting the expected user object fields.
    if ( ! isset( $user->user_email ) ) {
        // If you have a correct ID, you can still retrieve the user\'s fields.
        $user = get_user_by( \'ID\', $user->ID );
        // If the user doesn\'t exist, stop where you are.
        if ( ! $user ) {
            return false;
        }
    }

    $headers = array(
        \'Content-Type: text/html; charset=UTF-8\',
        \'From: WordPress Website <admin@example.com>\',
    );
    wp_mail( $user->user_email, \'Subject of email\', \'Email body\', $headers );
}

相关推荐

email address non required

我正在使用Woocommerce和Ionic,借助Woocommerce的API开发一个网站和应用程序。我试图让注册页面中的电子邮件字段成为可选字段,并让客户用他们的手机号码和密码注册。我不知道如何做到这一点,我应该让数据库中的email字段变为null吗,或者在我的情况下是否有任何解决方法?