PHP Developer

Monday, 12 October 2015

How can I change email Notification for new user in WordPress

When admin add a new customer from back end manualy the email notification goes to new user.

The new user notification email is created and sent by the function wp_new_user_notification(), found in wp-includes/plugable.php

There is no filter hook within this funciton that will allow you to manipulate the output of the email, however you can of course overwrite any pluggable function via a plugin.

Note - You can only overwrite pluggable functions from within a plugin, not from within your theme.

This code will create the plugin which will be used instead of the one in wp-includes/plugable.php


<?php
/**
 * Plugin Name: User Email Notification
 * Plugin URI: http://astro-global.blogspot.in/
 * Description: This plugin helps you to Send email notification to user if user create by admin in back end. Easily customize, send Username, Password and link also.
 * Version: 1.0.0
 * Author: Sunil Sharma
 * Author URI: https://plus.google.com/u/0/+SunilSharma_007
 */

$pathsss = $_SERVER['DOCUMENT_ROOT'];
if(!defined('DS')){
    define('DS',DIRECTORY_SEPARATOR);
}
include_once $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
function email_notification_activation()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
function email_notification_deactivation()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
function email_notification_uninstall()
{
    if ( ! current_user_can( 'activate_plugins' ) )
        return;
}
register_activation_hook( __FILE__, 'email_notification_activation' );
register_deactivation_hook( __FILE__, 'email_notification_deactivation' );
register_uninstall_hook( __FILE__, 'email_notification_uninstall' );

if ( !function_exists('wp_new_user_notification') ) :
/**
 * Pluggable - Email login credentials to a newly-registered user
 *
 * A new user registration notification is also sent to admin email.
 *
 * @since 2.0.0
 *
 * @param int    $user_id        User ID.
 * @param string $plaintext_pass Optional. The user's plaintext password. Default empty.
 */

function wp_new_user_notification($user_id, $plaintext_pass = '') {
    $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);

    $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(__('E-mail: %s'), $user->user_email) . "\r\n";

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

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
    $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
    //$message .= wp_login_url() . "\r\n";
    $message .= site_url() . "/sign-in/";

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

}
endif;
?>