Skip to content

Instantly share code, notes, and snippets.

@dhimaskirana
Created June 16, 2020 01:37
Show Gist options
  • Save dhimaskirana/7015bc49606d472aaac9346448b02834 to your computer and use it in GitHub Desktop.
Save dhimaskirana/7015bc49606d472aaac9346448b02834 to your computer and use it in GitHub Desktop.
<?php
function nws_email_template($message, $subject) {
// Email Template
$template_file = get_stylesheet_directory() . '/emails/default.php';
// Render Template
ob_start();
include_once( $template_file );
$nws_template = ob_get_contents();
ob_end_clean();
// Replace Placeholder
$message = str_replace('%%MAILCONTENT%%', $message, $nws_template);
$to_replace = array(
'%%BLOG_URL%%' => get_option( 'siteurl' ),
'%%HOME_URL%%' => get_option( 'home' ),
'%%BLOG_NAME%%' => get_option( 'blogname' ),
'%%BLOG_DESCRIPTION%%' => get_option( 'blogdescription' ),
'%%ADMIN_EMAIL%%' => get_option( 'admin_email' ),
'%%DATE%%' => date_i18n( get_option( 'date_format' ) ),
'%%TIME%%' => date_i18n( get_option( 'time_format' ) ),
'%%NWS%%' => $subject
);
foreach ($to_replace as $placeholder => $var ) {
if(is_array($var) ) {
do {
$var = reset($var);
} while(is_array($var));
}
$message = str_replace($placeholder, $var, $message);
}
// Return Template with data
return $message;
}
add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type($type) {
if( $type != 'text/html' ) {
// If not html, work with content and filter it
add_filter( 'nws_filter_email', 'wp_kses_post', 50 );
nws_content_filters();
}
return $content_type = 'text/html';
}
remove_filter('wp_mail_content_type', 'wp_mail_content_type');
add_filter( 'wp_mail', 'my_wp_mail_filter' );
function my_wp_mail_filter( $args ) {
// Remove [Neo Wedding Solution] to subject on email
$subject = preg_replace( '`\[[^\]]*\]`', '', $args['subject'] );
$args['message'] = nws_email_template(apply_filters('nws_filter_email', $args['message']), $subject);
return $args;
}
function nws_content_filters() {
add_filter( 'nws_filter_email', 'wptexturize' );
add_filter( 'nws_filter_email', 'convert_chars' );
add_filter( 'nws_filter_email', 'wpautop' );
add_filter( 'nws_filter_email', 'clean_retrieve_password' );
}
function clean_retrieve_password($message) {
return make_clickable( preg_replace( '@<(http[^> ]+)>@', '$1', $message ) );
}
add_filter( 'wp_mail_from_name', 'custom_wp_mail_from_name' );
function custom_wp_mail_from_name( $original_email_from ) {
return 'My Site Name';
}
add_filter( 'wp_mail_from', 'custom_wp_mail_from' );
function custom_wp_mail_from( $original_email_address ) {
//Make sure the email is from the same domain
//as your website to avoid being marked as spam.
//Just example email
return 'myemail@example.com';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment