admin管理员组

文章数量:1313599

When a new user signs up, the confirmation email is received and this is what it says:

Username: testuser To set your password, visit the following address:  

But I want to customize it, so I added the following code to the functions.php
but it does not work. The default email is still sent.

I am using WordPress 4.9.9

<?php
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 ) {
        
$wp_new_user_notification_email['subject'] = sprintf(__( '[%s] Your username and password' ), $blogname, $user->user_login );
        
$key = get_password_reset_key( $user );
$message = sprintf(__('Welcome to our website,')) . "\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 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;
}
?>

Why doesn't the code work? Would you help me?

When a new user signs up, the confirmation email is received and this is what it says:

Username: testuser To set your password, visit the following address:  

But I want to customize it, so I added the following code to the functions.php
but it does not work. The default email is still sent.

I am using WordPress 4.9.9

<?php
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 ) {
        
$wp_new_user_notification_email['subject'] = sprintf(__( '[%s] Your username and password' ), $blogname, $user->user_login );
        
$key = get_password_reset_key( $user );
$message = sprintf(__('Welcome to our website,')) . "\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 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;
}
?>

Why doesn't the code work? Would you help me?

Share Improve this question edited Dec 3, 2020 at 14:03 RyanS 1256 bronze badges asked Dec 22, 2018 at 9:43 Sh.DehnaviSh.Dehnavi 1093 silver badges18 bronze badges 6
  • @vikrant zilpe my friend i need the set password link. my users need to set their password – Sh.Dehnavi Commented Dec 22, 2018 at 9:50
  • @vikrant zilpe not reset password. after sing up in wordpress sites, it is received a mail to set your password – Sh.Dehnavi Commented Dec 22, 2018 at 9:55
  • please try this code : $message . = '<a href="' . network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login), 'login') . '">' . network_site_url("wp-login.php?action=rp&key=$adt_rp_key&login=" . rawurlencode($user_login), 'login') . '</a>' – vikrant zilpe Commented Dec 22, 2018 at 10:16
  • @vikrant it does not wrok :| – Sh.Dehnavi Commented Dec 22, 2018 at 11:17
  • what s problem ? – vikrant zilpe Commented Dec 22, 2018 at 11:21
 |  Show 1 more comment

1 Answer 1

Reset to default 1

There is actually nothing wrong with the code that you have (I tested it as you have it above and it works fine).

But you have to account for some additional possibilities:

  1. The wp_new_user_notification() function is a pluggable function. So it can be replaced by a plugin or by custom code. And if it is replaced, there is a possibility that the custom version of the function lacks the wp_new_user_notification_email filter hook.
  2. Possibly another filter already exists hooked to this same filter, but at a earlier priority (<10) and it redirects or otherwise exits the process. That would be bad form, but it is possible. This isn't all that likely the problem, but it is possible so that's why I mention it.
  3. The possibility exists you are targeting the wrong function. We're assuming that the email is the default WP registration, but you didn't state that specifically. If you're doing something where there's a custom registration and/or some other process, it doesn't necessarily go through wp_new_user_notification().

With those possibilities, I would recommend you do the following:

  1. Review your theme to make sure this is the only instance of a wp_new_user_notification_email filter.
  2. Disable ALL plugins.
  3. Re-test with all plugins deactivated.

本文标签: customizationWhat39s wrong with Customizing new user notification email by addfilter