Created
December 22, 2021 01:03
-
-
Save dhimaskirana/b1ea470b04b16b2f2cfe694f7ec37e01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Custom WP Email Template | |
* Plugin URI: https://www.dhimaskirana.com/cara-memasang-template-email-html-di-wordpress/ | |
* Description: Plugin sederhana untuk menggunakan custom html pada fungsi wp_mail() | |
* Version: 1.0 | |
* Requires at least: 5.2 | |
* Requires PHP: 7.2 | |
* Author: Dhimas Kirana | |
* Author URI: https://www.dhimaskirana.com/ | |
* License: GPL v2 or later | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
// Don't call the file directly | |
if (!defined('ABSPATH')) exit; | |
/** | |
* Custom HTML Template to wp_mail() | |
* | |
* Replace wpet_ with your prefix | |
* | |
* The HTML Template saved as custom-email-template.php in the same folder | |
* | |
*/ | |
function wpet_email_template($message) { | |
// Render Template | |
ob_start(); | |
include('custom-email-template.php'); | |
$wpet_template = ob_get_contents(); | |
ob_end_clean(); | |
// Replace Placeholder | |
$message = str_replace('%%MAILCONTENT%%', $message, $wpet_template); | |
// 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('wpet_filter_email', 'wp_kses_post', 50); | |
wpet_content_filters(); | |
} | |
return $content_type = 'text/html'; | |
} | |
add_filter('wp_mail', 'my_wp_mail_filter'); | |
function my_wp_mail_filter($args) { | |
$message = $args['message']; | |
$args['message'] = wpet_email_template(apply_filters('wpet_filter_email', $message)); | |
return $args; | |
} | |
function wpet_content_filters() { | |
add_filter('wpet_filter_email', 'wptexturize'); | |
add_filter('wpet_filter_email', 'convert_chars'); | |
add_filter('wpet_filter_email', 'wpautop'); | |
add_filter('wpet_filter_email', 'clean_retrieve_password'); | |
} | |
function clean_retrieve_password($message) { | |
return make_clickable(preg_replace('@<(http[^> ]+)>@', '$1', $message)); | |
} | |
add_action('phpmailer_init', 'wpet_email_return_path'); | |
function wpet_email_return_path($phpmailer) { | |
$phpmailer->Sender = $phpmailer->From; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment