注册新用户时,如何关闭用户和管理员的电子邮件通知?
我已经看到了一些建议和插件,但似乎都不起作用。一个是从一个插件中获取功能:
if ( !function_exists(\'wp_new_user_notification\') ) :
/**
* Notify the blog admin of a new user, normally via email.
*
* @since 2.0
*
* @param int $user_id User ID
* @param string $plaintext_pass Optional. The user\'s plaintext password
*/
function wp_new_user_notification($user_id, $plaintext_pass = \'\') {
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);
$message = sprintf(__(\'New user registration on your site %s:\'), $blogname) . "\\r\\n\\r\\n";
$message .= sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n\\r\\n";
$message .= sprintf(__(\'E-mail: %s\'), $user_email) . "\\r\\n";
@wp_mail(get_option(\'admin_email\'), sprintf(__(\'[%s] New User Registration\'), $blogname), $message);
if ( empty($plaintext_pass) )
return;
$message = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
$message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
$message .= wp_login_url() . "\\r\\n";
// wp_mail($user_email, sprintf(__(\'[%s] Your username and password\'), $blogname), $message)
}
endif;
这些问题和建议都很老了,所以WP 3.5可能有点过头了。
在注册时,我仍然会收到管理员电子邮件和发给用户的电子邮件。
但我不想阻止忘记密码的电子邮件。
SO网友:Eugene Manuilov
作用wp_new_user_notification
是可插拔的。这意味着您可以通过在插件/主题中声明此函数的版本来覆盖它。
因此,如果要完全禁用所有通知,请按以下方式执行:
if ( !function_exists( \'wp_new_user_notification\' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
return;
}
endif;
但是,我不建议您禁用所有通知,我建议您至少向用户发送通知(用户如何找到他的密码?)。因此,在这种情况下,您的代码应该如下所示:
if ( !function_exists( \'wp_new_user_notification\' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = \'\' ) {
$user = get_userdata( $user_id );
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$blogname = wp_specialchars_decode(get_option(\'blogname\'), ENT_QUOTES);
if ( empty($plaintext_pass) ) {
return;
}
$message = sprintf(__(\'Username: %s\'), $user_login) . "\\r\\n";
$message .= sprintf(__(\'Password: %s\'), $plaintext_pass) . "\\r\\n";
$message .= wp_login_url() . "\\r\\n";
wp_mail($user_email, sprintf(__(\'[%s] Your username and password\'), $blogname), $message);
}
endif;
SO网友:user119030
今天不得不这么做,发现其中许多解决方案似乎已经过时了。这看起来是一种不覆盖可插入函数的更好方法。这不是我的确切代码,但应该是一个很好的参考。
add_action( \'register_post\', \'maybe_stop_notifications\', 10, 3 );
function maybe_stop_notifications ( $sanitized_user_login, $user_email, $errors ) {
if( empty( $errors->get_error_code() )) {
remove_action( \'register_new_user\', \'wp_send_new_user_notifications\' );
}
}