Skip to content

Instantly share code, notes, and snippets.

@mohammadmursaleen
Last active December 29, 2023 15:21
Show Gist options
  • Save mohammadmursaleen/15a1d395ec7417526217179d7fb1ca25 to your computer and use it in GitHub Desktop.
Save mohammadmursaleen/15a1d395ec7417526217179d7fb1ca25 to your computer and use it in GitHub Desktop.
WordPress Plugin - Dynamically Generate Password and send in email Notification to Admin and New user
<?php
/*
Plugin Name: New Register Email Customizer
Description: Changes the copy in the email sent out to new users
Author: Mohammad Mursaleen
*/
// Redefine user notification function
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' );
}
$user = get_userdata( $user_id );
// Generate password
$password = wp_generate_password( 12, false );
wp_set_password( $password, $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";
$message .= sprintf( __( 'Password: %s' ), $password ) . "\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;
}
$switched_locale = switch_to_locale( get_user_locale( $user ) );
$message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n";
$message .= get_home_url(). '/wp-login.php/' . "\n";
$message .= sprintf(__('Username: %s'), $user->user_login) . "\n";
$message .= sprintf(__('Password: %s'), $password ) . "\n\n";
$message .= sprintf(__('If you have any problems, please contact us at %s.'), get_option('admin_email')) . "\n\n";
wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message);
if ( $switched_locale ) {
restore_previous_locale();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment