求助:移除WordPress注册邮件的用户名与登录URL,代码无效
Let’s work through why your custom registration email isn’t overriding the default one. Here are the most likely fixes and explanations:
1. Boost Your Hook Priority
Your current code uses the default priority of 10, but other plugins or your theme might be using the same hook with a higher priority (which runs later and overwrites your changes). Try bumping the priority to 20 or higher to ensure your code executes last:
add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 20, 3 ); function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) { // Fixed the typo "registerd" to "registered" while we're at it $wp_new_user_notification_email['subject'] = sprintf( '[%s] New user %s registered.', $blogname, $user->user_login ); $wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registered to your blog %s.", $user->user_login, $user->user_email, $blogname ); // Optional debug line to confirm your code runs (check wp-content/debug.log later) error_log( 'Custom registration email filter ran for user ID: ' . $user->ID ); return $wp_new_user_notification_email; }
2. Double-Check Where You’re Adding the Code
Make sure your code is placed in a location that loads properly:
- Your active child theme’s
functions.phpfile (never edit the parent theme directly—updates will erase your changes) - A custom plugin file (the most reliable option, as it’s independent of theme updates)
If you’re using a parent theme without a child, create a child theme first or use a custom plugin to hold your code.
3. Rule Out Plugin/Theme Conflicts
Third-party plugins (like membership tools, custom registration forms, or security plugins) often override WordPress’s default registration email system. To test this:
- Disable all plugins except WordPress core
- Switch to a default theme (e.g., Twenty Twenty-Four)
- Register a test user and see if your custom email appears
If it works, re-enable plugins one by one to find the conflicting tool.
4. Confirm WordPress Version Compatibility
The wp_new_user_notification_email filter was introduced in WordPress 5.5. If you’re running an older version, this hook won’t exist. You have two options:
- Update WordPress to the latest stable version (highly recommended for security and functionality)
- Use the older (deprecated but functional)
wp_new_user_notificationfunction override for pre-5.5 versions:
if ( ! function_exists( 'wp_new_user_notification' ) ) { function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) { $user = get_userdata( $user_id ); $blogname = wp_specialchars_decode( get_option('blogname'), ENT_QUOTES ); // Send custom email to the new user $subject = sprintf( '[%s] New user %s registered.', $blogname, $user->user_login ); $message = sprintf( "%s ( %s ) has registered to your blog %s.", $user->user_login, $user->user_email, $blogname ); wp_mail( $user->user_email, $subject, $message ); // Optional: Keep sending admin notification if needed if ( 'admin' === $notify || empty( $notify ) ) { $admin_subject = sprintf( '[%s] New User Registration', $blogname ); $admin_message = sprintf( "New user %s (%s) registered.", $user->user_login, $user->user_email ); wp_mail( get_option('admin_email'), $admin_subject, $admin_message ); } } }
5. Debug with Logs
Add the error_log() line from the first code snippet, then enable debug logging in wp-config.php by adding:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true );
Check the wp-content/debug.log file after registering a user—if you don’t see your debug entry, your code isn’t being loaded at all.
内容的提问来源于stack exchange,提问作者DrMTR

