Skip to content

Instantly share code, notes, and snippets.

@krasenslavov
Last active April 26, 2020 15:33
Show Gist options
  • Save krasenslavov/afd0cf847430744b5bc624c34b8fe892 to your computer and use it in GitHub Desktop.
Save krasenslavov/afd0cf847430744b5bc624c34b8fe892 to your computer and use it in GitHub Desktop.
Sending custom emails in WordPress. Visit blog post https://bit.ly/2xlZG9L
<?php
add_action('wp_mail_content_type', function() {
return "text/html";
}, 10, 1);
add_action('retrieve_password_message', function($message, $reset_key, $user_login, $user_data) {
$logo_url = esc_url('https://cdn-domain-name.com/assets/img/logo.png');
$featured_url = esc_url('https://cdn-domain-name.com/assets/img/featured-image.png');
$reset_url = esc_url(home_url('/wp-login.php?action=rp&key=') . $reset_key . '&login=' . $user_login);
compose_email(
array(
'title' => __('Password reset requested.', 'textdomain'),
'content' => html_entity_decode(sprintf(esc_html__('<p>%1$s,<strong> ' . $user_data->display_name . '</strong></p><p>%2$s</p><p>%3$s<br />'. $reset_url . '</p>'),
__('Hi', 'textdomain'), // 1
__('You have requested a password reset! If this was a mistake, just ignore this email and nothing will happen.', 'textdomain'), // 2
__('To reset your password please click on the button bellow or copy &amp; paste the link to enter your new password', 'textdomain') // 3
)),
'logo_url' => $logo_url,
'featured_image_url' => $featured_url,
'cta_button_url' => $reset_url,
'cta_button_text' => __('Reset your password', 'textdomain')
),
$user_data->user_email
);
}, 10, 4);
add_action('wp_new_user_notification_email', function($email_data, $user) {
$reset_key = get_password_reset_key($user);
$logo_url = esc_url('https://cdn-domain-name.com/assets/img/logo.png');
$featured_url = esc_url('https://cdn-domain-name.com/assets/img/featured-image.png');
$reset_url = esc_url(home_url('/wp-login.php?action=rp&key=') . $reset_key . '&login=' . $user->data->user_login);
return overwrite_email(
$email_data,
array(
'title' => __('Welcome to Company Name!', 'textdomain'),
'content' => html_entity_decode(sprintf(esc_html__('<p>%1$s,<strong> ' . $user->data->display_name . '</strong>%2$s</p><p>%3$s</p><p>%4$s<br />' . $reset_url . '</p>'),
__('Hi', 'textdomain'), // 1
__('and thank you for joining us!', 'textdomain'), // 2
__('You have successfully registered an account.', 'textdomain'), // 3
__('Click on the button bellow or copy &amp; paste the link to veriy your email address and enter your new password.', 'textdomain') // 4
)),
'logo_url' => $logo_url,
'featured_image_url' => $featured_url,
'cta_button_url' => $reset_url,
'cta_button_text' => __('Reset your password', 'textdomain')
)
);
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment