首页 > 解决方案 > 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');

标签: phpwordpress

解决方案


推荐阅读