Skip to content

Instantly share code, notes, and snippets.

@markbiek
Created January 17, 2017 16:12
Show Gist options
  • Save markbiek/859fcd7030e98554b510797338469567 to your computer and use it in GitHub Desktop.
Save markbiek/859fcd7030e98554b510797338469567 to your computer and use it in GitHub Desktop.
add_filter('wp_mail', 'handle_wp_mail');
function handle_wp_mail($args) {
if ($args['subject'] == 'Welcome to YOUR SITE') {
$wpdb = $GLOBALS['wpdb'];
$email = $args['to'][0];
$user = get_user_by('email', $email);
//Generate a new user_activation_key (eg password reset key)
//This allows us to create a link that a new user can click to set their initial password
$key = wp_generate_password(20, false);
require_once(ABSPATH . WPINC . '/class-phpass.php');
$hasher = new PasswordHash(8, true);
$hashed = time() . ':' . $hasher->HashPassword($key);
$wpdb->update($wpdb->users, ['user_activation_key' => $hashed], ['user_login' => $user->user_login]);
$regUrl = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login');
$newMessage = <<<EOT
<h2>Welcome to YOUR SITE</h2>
<p>Dear User,</p>
<p>Thanks for registering!</p>
<p><a href="$regUrl">Click here to set your password.</a></p>
EOT;
$newMail = [
'to' => $args['to'],
'subject' => $args['subject'],
'headers' => $args['headers'],
'attachments' => $args['attachments'],
'message' => $newMessage,
];
return $newMail;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment