4

I am trying to customize the notification email sent to user that contains the link to set password.

Currently, I have set up a custom user registration and login forms at the urls site.com/register and site.com/login respectively.

After the registration, wordpress is sending email with following link that asks to set a password.

site.com/wp-login.php?action=rp&key=XYieERXh3QinbU4VquB2&login=user%40gmail.com

I want it replace this url to

site.com/login?action=rp&key=XYieERXh3QinbU4VquB2&login=user%40gmail.com

I have tried the following code in functions.php

add_filter( 'wp_new_user_notification_email', 'my_wp_new_user_notification_email', 10, 3 );

function my_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {

    $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
    $message .= __('Hello To set your password, visit the following address:') . "\r\n\r\n";
    $message .= '<' . network_site_url("login/?key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n";


    $wp_new_user_notification_email['message'] = $message

    return $wp_new_user_notification_email;

}

I think I am missing $key information since the email received has the link like this:

site.com/login?action=rp&key=&login=user%40test.com

How to fix this?

2
  • It doesn't work for me, after pasting it in my Child theme function.php. Could you help me ? – Aurélien Dec 12 '18 at 18:14
  • @Aurélien this isn't a discussion forum site, it's a Question/Answers site, so post comments as comments, not as answers – Tom J Nowell Dec 12 '18 at 18:48
4

You need to use the get_password_reset_key function (see reference) which will query the database and return the reset password key of the user.

Here's a full working example:

add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $key = get_password_reset_key( $user );
    $message = sprintf(__('Welcome to the Community,')) . "\r\n\r\n";
    $message .= 'To set your password, visit the following address:' . "\r\n\r\n";
    $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n";
    $message .= "After this you can enjoy our website!" . "\r\n\r\n";
    $message .= "Kind regards," . "\r\n";
    $message .= "Support Office Team" . "\r\n";
    $wp_new_user_notification_email['message'] = $message;

    $wp_new_user_notification_email['headers'] = 'From: MyName<example@domain.ext>'; // this just changes the sender name and email to whatever you want (instead of the default WordPress <wordpress@domain.ext>

    return $wp_new_user_notification_email;
}
3
  • Some explanation why your code would solve OP's question would be nice. – cjbj Oct 24 '18 at 12:02
  • I edited my answer. Do you think it's ok now? – Dimitris Siakavelis Oct 24 '18 at 12:14
  • Yes, this is better. Now you have explained the key component of your solution. Thank you. – cjbj Oct 24 '18 at 12:16
1

You need to be able to assign the generated key to your variable $key, which is currently undefined.

You could work around this by attaching another function to the retrieve_password_key action, which fires just after WordPress generates the key. From its name I would think it exists just for this purpose.

function wpse306642_stash_key( $user_login, $key ) {
    global $wpse306642_new_user_key;
    $wpse306642_new_user_key = $key;
}

add_action( 'retrieve_password_key', 'wpse306642_stash_key', 10, 2 );

Then replace $key in your function with $wpse306642_new_user_key, declaring it as global at the start of the function.

Using a global just for this does feel a little hacky, but it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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