Support » Fixing WordPress » Unable to register a custom welcome email

  • Resolved Archie22is

    (@archie22is)


    Hello peeps,

    I have been battling to register a custom welcome email. Basically, you submit a form to us via email and then we do a vet and then create a login account manually for you in the WordPress dashboard and assign a “dealer” role.

    The process is lengthy but works. Anyway, I want to be able to overwrite the following email:

    Username: xxx@gmail.com
    
    To set your password, visit the following address:
    
    https://development.xxx.com/wp-login.php?action=rp&key=FlFdsDeAveg2EN1HuqGB0G&login=xxx%40gmail.com
    
    https://development.xxx.com/wp-login.php

    With a custom email 🙂

    I have the following code but it’s not working…

    /**
     * Custom Welcome Email
     * @author Archie M
     *
     */
    
    add_filter( 'wpmu_signup_user_notification_subject', 'my_activation_subject', 10, 4 );
    
    function my_activation_subject( $text ) {
    
        return 'Welcome to xxx! (Activation required)';
    }
    
    add_filter('wpmu_signup_user_notification_email', 'my_custom_email_message', 10, 4);
    
    function my_custom_email_message($message, $user, $user_email, $key) {
    
    //Here is the new message:
    
        $message = sprintf(__('Hi %s'), $user->display_name) . "\r\n\r\n";
        $message .= __('Your registration on xxx has been approved.') . "\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";
    
        return sprintf($message);
    
    }

    What am I missing? The emails are created by an admin in the dashboard manually.

Viewing 1 replies (of 1 total)
  • Archie22is

    (@archie22is)

    Amazing. I rewrote half of the default function to accomplish what I wanted. I am pretty sure that there is a solution out there:

    add_filter( 'wp_new_user_notification_email' , 'edit_user_notification_email', 10, 3 );
    
    function edit_user_notification_email( $wp_new_user_notification_email, $user, $user_email ) {
    
        global $wpdb, $wp_hasher;
    
        $key = wp_generate_password( 20, false );
    
        /** This action is documented in wp-login.php */
        do_action( 'retrieve_password_key', $user->user_login, $key );
    
        // Now insert the key, hashed, into the DB.
        if ( empty( $wp_hasher ) ) {
            require_once ABSPATH . WPINC . '/class-phpass.php';
            $wp_hasher = new PasswordHash( 8, true );
        }
        $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
        $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
    
        $switched_locale = switch_to_locale( get_user_locale( $user ) );
    
        // send message
        $message = sprintf(__('Hi %s'), $user->display_name) . "\r\n\r\n";
        $message .= __('Your registration on xxx.com has been approved.') . "\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;
    
        return $wp_new_user_notification_email;
    
    }

    You can further extend $wp_new_user_notification_email() as indicated below:

    $wp_new_user_notification_email = array(
            'to'      => $user->user_email,
            /* translators: Password change notification email subject. %s: Site title */
            'subject' => __( '[%s] Your username and password info' ),
            'message' => $message,
            'headers' => '',
        );
Viewing 1 replies (of 1 total)
  • The topic ‘Unable to register a custom welcome email’ is closed to new replies.