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.


I want it replace this url to


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:


How to fix this?

  • It doesn't work for me, after pasting it in my Child theme function.php. Could you help me ?
    – Aurélien
    Dec 12, 2018 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, 2018 at 18:48

2 Answers 2


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<[email protected]>'; // this just changes the sender name and email to whatever you want (instead of the default WordPress <[email protected]>

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

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 and acknowledge you have read our privacy policy.

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