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 | Show 1 more comment1 Answer
Reset to default 1There 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:
- 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 thewp_new_user_notification_email
filter hook. - 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.
- 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:
- Review your theme to make sure this is the only instance of a
wp_new_user_notification_email
filter. - Disable ALL plugins.
- Re-test with all plugins deactivated.
本文标签: customizationWhat39s wrong with Customizing new user notification email by addfilter
版权声明:本文标题:customization - What's wrong with Customizing new user notification email by add_filter? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741943910a2406302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
set password link
. my users need to set their password – Sh.Dehnavi Commented Dec 22, 2018 at 9:50