0

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;
  
}
1
  • based on information from this post, it seems like it would make the most sense to change the header to HTML from text/plain. I would have to do this before the wp_mail() function is ran by the original function. Apr 11, 2023 at 17:16

1 Answer 1

0

I think you're looking for the wp_new_user_notification_email() filter like here.

3
  • This is helpful. Am I still able to simple add to or remove content from the variable $message? Apr 7, 2023 at 17:22
  • I looked into this a bit further and tried putting this in my functions.php, after deleting all of my old code. Now, there is no fatal error, but it still does not work, and this seems to prevent any email from being sent. add_filter( 'wp_new_user_notification_email', 'mtk_custom_wp_new_user_notification_email', 10 ); function mtk_custom_wp_new_user_notification_email() { $message = "The administrator reviewed your request to have an account created on our site, and approved your request. Here is your new username, and a link to set the password for your account." . $message; } Apr 7, 2023 at 18:04
  • You have to return the $message from the function.
    – moped
    Apr 8, 2023 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.