php - Wordpress:我有一个自定义更改电子邮件表单,如何发送确认电子邮件
问题描述
我的 functions.php 中有以下代码,用于为用户生成一个表单,以便从前端更改他们的帐户电子邮件。它可以工作,但我希望它发送一封标准的确认电子邮件,Wordpress 通常在更改电子邮件时执行此操作,而不是立即更改它。这是简单的,还是困难的?
function change_email_form() {
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$user_email = $user_info->user_email;
?>
<form id="updateemail" action="#updateemail" method="post">
<input id="new_email" type="text" name="new_email" title="new_email" placeholder="<?php echo $user_email; ?>" required>
<button class="submit" type="submit">Update Email</button>
</form>
<?php }
function change_email(){
if(isset($_POST['new_email'])){
$_POST = array_map('stripslashes_deep', $_POST);
$new_email = sanitize_email($_POST['new_email']);
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$user_email = $user_info->user_email;
$errors = array();
$current_user = get_user_by('id', $user_id);
// Check for errors
if ( $user_email == $new_email ) {
$errors[] = 'Address unchanged';
}
if ( ! is_email( $new_email ) ) {
$errors[] = 'Address invalid';
}
if ( email_exists( $new_email ) ) {
$errors[] = 'Address unavailable';
}
if(empty($errors)){
$args = array(
'ID' => $user_id,
'user_email' => $new_email,
);
wp_update_user( $args );
send_confirmation_on_profile_email();
echo '<small class="notice">Address updated</small>';
} else {
// Echo Errors
foreach($errors as $error){
echo '<p>';
echo '<small class="notice">';
echo $error;
echo '</small>';
}
}
}
}
function email_form_shortcode(){
change_email();
change_email_form();
}
add_shortcode('changeemail_form', 'email_form_shortcode');
解决方案
推荐阅读
- datetime - 如何在颤动中使用```DateTime```显示月份
- python - 从excel读取数据时如何避免unicode问题
- mongoexport - 我在哪里可以找到 suse os 中的 mongoexport 命令?
- python - 如何根据列日期查找所选行的最大值
- android - 使用 ndk-build 时自定义宏
- laravel - Laravel 5.4,在返回提交表单的视图时,出现错误;“标题可能不包含多个标题,检测到新行”
- alteryx - 使用文件名将 CSV 文件加载到 Azure 表中
- java - 更改 Multimap 中显示的值的顺序
- node.js - 具有清晰上传和调整大小的 Multer-S3
- node.js - 如何在 Mongoose 填充查询中获取计数