Custom Link in Password Email

User
link text
add before clicktracking=off href in wordpress password reset email
Assistant
To add a custom attribute like `clicktracking=off` to the password reset email links in WordPress, you can use the `retrieve_password_message` filter. This allows you to modify the email message content before it is sent out. Here's how to do it:

1. **Add the Code to Your Theme or Plugin:**
You can add the following code to your theme's `functions.php` file or a custom plugin:

function custom_retrieve_password_message($message, $key, $user_login) {
       $site_url = site_url('wp-login.php?action=rp&key=' . $key . '&login=' . rawurlencode($user_login));
       $custom_link = '<a clicktracking=off href="' . esc_url($site_url) . '">Reset Password</a>';
       
       // Modify the message to include the custom link
       $message = str_replace('%%PASSWORD_RESET_LINK%%', $custom_link, $message);
       
       return $message;
   }
   add_filter('retrieve_password_message', 'custom_retrieve_password_message', 10, 3);

2. **Modify the Default Message:**
If you want to include a placeholder like `%%PASSWORD_RESET_LINK%%` in the default password reset message, you can customize the default message using the `retrieve_password_message` filter. You can also update the message directly:

function custom_password_reset_email($message, $key, $user_login) {
       $reset_link = site_url('wp-login.php?action=rp&key=' . $key . '&login=' . rawurlencode($user_login));
       $message = sprintf(__('Someone has requested a password reset for the following account: %s'), $user_login) . "nn";
       $message .= __('Username:') . " " . $user_login . "n";
       $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "nn";
       $message .= __('To reset your password, visit the following address:') . "n";
       $message .= '<a clicktracking=off href="' . esc_url($reset_link) . '">' . __('Reset Password') . '</a>' . "n";

       return $message;
   }
   add_filter('retrieve_password_message', 'custom_password_reset_email', 10, 3);

3. **Testing:**
After adding the code, test the password reset functionality by trying to reset your password. The email should now contain the reset link with the `clicktracking=off` attribute.

Make sure to back up your theme or use a child theme before making any changes to `functions.php`. If you're using a custom plugin, you can add the code there instead.

Leave a Comment

Your email address will not be published. Required fields are marked *