11

I'm just wondering why wordpress doesn't send confirmation mail every time user change his/her email address.

How do we know that email address is not fake or mistyped?

So can anyone give me some snippet to implement this function?

Update:

Here is the idea.

  1. user changes his/her mail
  2. We send confirmation email.
  3. If the user confirm that email in X days by clicking confirmation link, then the email should be changed. Else we should use existing email.
3
  • So if the user changes his/her email do they get logged out and not allowed back in until they re-verify their email address?
    – Scott
    Commented Feb 9, 2012 at 11:49
  • No, That will be bad. What if the user mistype the email address and get logged out? He/she can't verify it. So he/she will be blocked forever. We just giving warning message to verify his/her email address. If the user not verify his/her email in X hours (Say 24 hours) then the already verified mail should be used. Commented Feb 9, 2012 at 13:48
  • Please put these details in the question.
    – Scott
    Commented Feb 9, 2012 at 15:52

5 Answers 5

9
+50

Like SickHippie posted this functionality is native to WordPress but only for a multisite setup so here is the two functions you need to get this to work on a single site setup which are mostly code one for one from the core /wp-admin/user-edit.php file

function custom_send_confirmation_on_profile_email() {
    global $errors, $wpdb;
    $current_user = wp_get_current_user();
    if ( ! is_object($errors) )
        $errors = new WP_Error();

    if ( $current_user->ID != $_POST['user_id'] )
        return false;

    if ( $current_user->user_email != $_POST['email'] ) {
        if ( !is_email( $_POST['email'] ) ) {
            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
            return;
        }

        if ( email_exists( $_POST['email'] ) ) {
            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
            delete_user_meta( $current_user->ID . '_new_email' );
            return;
        }

        $hash = md5( $_POST['email'] . time() . mt_rand() );
        $new_user_email = array(
            'hash' => $hash,
            'newemail' => $_POST['email']
        );
        update_user_meta( $current_user->ID . '_new_email', $new_user_email );

        $content = apply_filters( 'new_user_email_content', __( "Dear user,

    You recently requested to have the email address on your account changed.
    If this is correct, please click on the following link to change it:
    ###ADMIN_URL###

    You can safely ignore and delete this email if you do not want to
    take this action.

    This email has been sent to ###EMAIL###

    Regards,
    All at ###SITENAME###
    ###SITEURL###" ), $new_user_email );

        $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
        $content = str_replace( '###EMAIL###', $_POST['email'], $content);
        $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
        $content = str_replace( '###SITEURL###', home_url(), $content );

        wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
        $_POST['email'] = $current_user->user_email;
    }
}
add_action( 'personal_options_update', 'custom_send_confirmation_on_profile_email' );

// Execute confirmed email change. See send_confirmation_on_profile_email().
function verify_email_change(){
    global $errors, $wpdb;
    $current_user = wp_get_current_user();
    if (in_array($GLOBALS['pagenow'], array('profile.php')) && $current_user->ID > 0) {
        if (isset( $_GET[ 'newuseremail' ] ) && $current_user->ID ) {
            $new_email = get_user_meta( $current_user->ID . '_new_email' );
            if ( $new_email[ 'hash' ] == $_GET[ 'newuseremail' ] ) {
                $user->ID = $current_user->ID;
                $user->user_email = esc_html( trim( $new_email[ 'newemail' ] ) );
                if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->users} WHERE user_login = %s", $current_user->user_login ) ) )
                    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
                wp_update_user( get_object_vars( $user ) );
                delete_user_meta( $current_user->ID . '_new_email' );
                wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
                die();
            }
        } elseif ( !empty( $_GET['dismiss'] ) && $current_user->ID . '_new_email' == $_GET['dismiss'] ) {
            delete_user_meta( $current_user->ID . '_new_email' );
            wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
            die();
        }
    }
}
add_action('plugins_loaded','verify_email_change');
1
  • Hi, your verify_email_change function contains multisite related codes ({$wpdb->signups). Can you fix it? Commented Dec 13, 2012 at 6:21
3

This is a strange 'feature'. The function is actually available inside WordPress (WordPress.com has it enabled for their managed blog service), but it is restricted to multisite. If you look in /wp-admin/includes/ms.php you'll find the function that handles this - line 239 send_confirmation_on_profile_email().

Presumably, you could move this function into your functions.php or into a plugin to gain this functionality, possibly with a bit of tweaking to get it to work right. It doesn't answer the "why", but neither does the trac ticket on this subject here.

ETA: Looking further into it, there's a few other functions you may need to duplicate as well - new_user_email_admin_notice() and update_option_new_admin_email() jump out as potentially necessary.

2

Giri's response didn't work for me. I had to tweak mine to make it work (Wordpress 3.5)

function cleanup_verify_email_change()
{
    global $errors, $wpdb;
    $current_user = wp_get_current_user();

    // don't execute this if they're trying to dismiss a pending email change
    if (in_array($GLOBALS['pagenow'], array('profile.php')) && $current_user->ID > 0 & !isset($_GET["dismiss"])) 
    {
        if (isset( $_POST[ 'email' ] ) && ($current_user->user_email != $_POST['email']) ) 
        {
            $user->ID = $current_user->ID;
            $user->user_email = esc_html( trim( $_POST[ 'email' ] ) );

            if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->users} WHERE user_login = %s", $current_user->user_login ) ) ) {
                $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
            }

            wp_update_user( get_object_vars( $user ) );

            wp_redirect( add_query_arg( array('updated' => 'true', 'multisite_cleanup' => 'true'), self_admin_url( 'profile.php' ) ) );
            die();
        } 
        elseif ( !empty( $_GET['dismiss'] ) && $current_user->ID . '_new_email' == $_GET['dismiss'] ) 
        {
            delete_user_meta( $current_user->ID . '_new_email' );
            wp_redirect( add_query_arg( array('updated' => 'true', 'multisite_cleanup' => 'true'), self_admin_url( 'profile.php' ) ) );
            die();
        }
    }
}
add_action('plugins_loaded','cleanup_verify_email_change');
0

I've tweaked Giri code so that it works on my wordpress (version 4.8.1+)

before:

 update_user_meta( $current_user->ID . '_new_email', $new_user_email );

after:

 update_user_meta( $current_user->ID, '_new_email', $new_user_email );

The comma needs to replace the period.

Also:

$new_email['hash'];
$new_email['newemail'];

became

$new_email[0]['hash'];
$new_email[0]['newemail'];

Therefore:

function custom_send_confirmation_on_profile_email() {
    global $errors, $wpdb;
    $current_user = wp_get_current_user();
    if ( ! is_object($errors) )
        $errors = new WP_Error();

    if ( $current_user->ID != $_POST['user_id'] )
        return false;

    if ( $current_user->user_email != $_POST['email'] ) {
        if ( !is_email( $_POST['email'] ) ) {
            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address isn't correct." ), array( 'form-field' => 'email' ) );
            return;
        }

        if ( email_exists( $_POST['email'] ) ) {
            $errors->add( 'user_email', __( "<strong>ERROR</strong>: The e-mail address is already used." ), array( 'form-field' => 'email' ) );
            delete_user_meta( $current_user->ID, '_new_email' );
            return;
        }

        $hash = md5( $_POST['email'] . time() . mt_rand() );
        $new_user_email = array(
            'hash' => $hash,
            'newemail' => $_POST['email']
        );
        update_user_meta( $current_user->ID, '_new_email', $new_user_email );

        $content = apply_filters( 'new_user_email_content', __( "Dear user,

        You recently requested to have the email address on your account changed.
        If this is correct, please click on the following link to change it:
        ###ADMIN_URL###

        You can safely ignore and delete this email if you do not want to
        take this action.

        This email has been sent to ###EMAIL###

        Regards,
        All at ###SITENAME###
        ###SITEURL###" ), $new_user_email );

        $content = str_replace( '###ADMIN_URL###', esc_url( admin_url( 'profile.php?newuseremail='.$hash ) ), $content );
        $content = str_replace( '###EMAIL###', $_POST['email'], $content);
        $content = str_replace( '###SITENAME###', get_site_option( 'site_name' ), $content );
        $content = str_replace( '###SITEURL###', home_url(), $content );

        wp_mail( $_POST['email'], sprintf( __( '[%s] New Email Address' ), get_option( 'blogname' ) ), $content );
        $_POST['email'] = $current_user->user_email;
    }
}
add_action( 'personal_options_update', 'custom_send_confirmation_on_profile_email' );

// Execute confirmed email change. See send_confirmation_on_profile_email().
function verify_email_change(){
    global $errors, $wpdb;
    $current_user = wp_get_current_user();
    if (in_array($GLOBALS['pagenow'], array('profile.php')) && $current_user->ID > 0) {
        if (isset( $_GET[ 'newuseremail' ] ) && $current_user->ID ) {
            $new_email = get_user_meta( $current_user->ID, '_new_email' );
            if ( $new_email[0]['hash'] == $_GET[ 'newuseremail' ] ) {
                $user->ID = $current_user->ID;
                $user->user_email = esc_html( trim( $new_email[0][ 'newemail' ] ) );
                if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->users} WHERE user_login = %s", $current_user->user_login ) ) )
                    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
                wp_update_user( get_object_vars( $user ) );
                delete_user_meta( $current_user->ID, '_new_email' );
                wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
                die();
            }
        } elseif ( !empty( $_GET['dismiss'] ) && $current_user->ID . '_new_email' == $_GET['dismiss'] ) {
            delete_user_meta( $current_user->ID, '_new_email' );
            wp_redirect( add_query_arg( array('updated' => 'true'), self_admin_url( 'profile.php' ) ) );
            die();
        }
    }
}
add_action('after_setup_theme','verify_email_change');

Cheers.

0

WP Update 6 dropped a while ago, and this is still an issue to me. None of the previous solutions work, and the code is literally written in the wp-include/user.php file, but there is no option to enable such a feature. Did anyone found out how to enable it?

2
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – vancoder
    Commented Jun 6, 2022 at 21:50
  • Thanks for the correction and I apologize for using the question incorrectly
    – mnrlab
    Commented Jun 7, 2022 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.