I am looking to customize the "Login Details"
email sent by WP Core when an administrator checks off "Send User Notification"
while manually creating a user from /wp-admin/user-new.php
. How can I format and style this email to match other parts of my theme? The email seems to be created in /wp-includes/pluggable.php, between lines 2093 and 2233, by a function called "wp_new_user_notification". To my understanding, pluggable.php is loaded BEFORE my theme, though I want this to be a part of my theme, and run from functions.php.
Here is the section of code that I am looking to edit/filter:
/* translators: %s: User login. */
$message = sprintf( __( 'Username: %s' ), $user->user_login ) .
$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 .= wp_login_url() . "\r\n";
$wp_new_user_notification_email = array(
'to' => $user->user_email,
/* translators: Login details notification email subject. %s: Site title. */
'subject' => __( '[%s] Login Details' ),
'message' => $message,
'headers' => '',
);
What I initially tried to edit this was similar to what was mentioned in this post by Sh.Dehnavi, though since I am attempting to add in some HTML/CSS for formatting and styling, it ended up being different in practice, and the method I used produced a fatal error.
/* translators: %s: User login. */
$message = ?>
<div id="email-wrapper">
<?;
$message .= sprintf( __( 'Username: %s' ), $user->user_login ) .
$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 .= ?> </div> <?;
// Above didn't work, so I tried below instead, which also produced a fatal error
$message = echo "<div";
$message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\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 .= wp_login_url() . "\r\n";
$message .= echo "</div>";
After researching online, I found that you can filter these using add_filter , which I was able to add to my functions.php file. I am still having the problem that this does not accept HTML code. I believe that it may be possible to convert this email to an HTML email using the wp_mail_content_type() filter. This is what my code looks like currently, before adding in that filter, and this does work to change the content of the email, but it does not style it as I am looking for.
add_filter( 'wp_new_user_notification_email', 'mtk_custom_wp_new_user_notification_email', 10, 3 );
function mtk_custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
$wp_new_user_notification_email['subject'] = sprintf( 'Your New MarTek Prober Account Has Been Created!', $blogname, $user->user_login );
$message = __( 'You are receiving this email because we have approved your request to have an account made for our webstore.' ) . "\r\n";
$message .= __( '--' ) . "\r\n\r\n";
$message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\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";
$wp_new_user_notification_email['message'] = $message;
$wp_new_user_notification_email['headers'] = "From: Acme Co <[email protected]>";
return $wp_new_user_notification_email;
}