damienoneill2001
damienoneill2001

Reputation: 499

Edit the WordPress Activation Email

Is it possible to amend the content of the Activation email that is sent to users when they sign up for an account on a WordPress site?

I am looking to change the subject and the body of the email to read something a little more in line with my website's information.

I have tried to added the following into a custom plugin found inside mu-plugins, but it seems to have no affect at all:

<?php
/*
Plugin Name: Disable Username and Password Notification
Description: Disables the Username and Password email
*/
// Start changing email body
function myprefix_change_activation_email_body ($old_body, $domain, $path, $title, $user, $user_email, $key, $meta) {
    $my_message = "Hi! This is my new email! ";
    $my_message .= "Your site {$title} is almost ready. We probably also want to include the activation key: {$key} ";
    $my_message .= "Activate your site here : http://{$domain}{$path}wp-activate.php?key=$key";
    // ... other stuff
    return $my_message;
}
add_filter('wpmu_signup_blog_notification_email', 'myprefix_change_activation_email_body', 10, 8);
// End changing email body

// Start changing email subject
function myprefix_change_activation_email_subject ($old_subject, $domain, $path, $title, $user, $user_email, $key, $meta) {
    $my_subject = "Hi! You just registered on my site!";
    return $my_subject;
}
add_filter('wpmu_signup_blog_notification_subject', 'myprefix_change_activation_email_subject', 10, 8);
// End changing email subject

From what I can tell, the issue is that the filter being used is most likely for MultiSite setups which my site is not. It is just a standard WordPress site.

Cheers

Upvotes: 0

Views: 3618

Answers (1)

Marcos Rodrigues
Marcos Rodrigues

Reputation: 712

The function that you're looking for is wp_new_user_notification it's a pluggable function. Pluggable functions are function that let you override Wordpress Core functionalities.

You can override, simply by:

  1. Create a file in mu-plugins folder, you can call whatever you want, but you should give a meaningful name, like custom-new-user-notification.php
  2. Copy the default function and wrapping it in a if statement checking if the function doesn't exist. (it's a best practice if not it can produce errors)
  3. Change the message and subject ( or whatever you want to change)

Example:

   <?php
        /*
        Plugin Name:     Custom User Notification
        Plugin URI:      http://www.example.com
        Description:     Pligin description
        Version:         1.0
        Author:          Your Name
        Author URI:      http://www.authorurl.com
        */


        if( !function_exists( 'wp_new_user_notification' ) ){

            function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
                if ( $deprecated !== null ) {
                    _deprecated_argument( __FUNCTION__, '4.3.1' );
                }

                global $wpdb, $wp_hasher;
                $user = get_userdata( $user_id );

                // 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);

                if ( 'user' !== $notify ) {
                    $switched_locale = switch_to_locale( get_locale() );
                    $message  = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
                    $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n";

                    @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message );

                    if ( $switched_locale ) {
                        restore_previous_locale();
                    }
                }

                // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification.
                if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) {
                    return;
                }

                // Generate something random for a password reset key.
                $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 ) );

                $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";

                wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message);

                if ( $switched_locale ) {
                    restore_previous_locale();
                }
            }

        }

Upvotes: 3

Related Questions