Last active
February 21, 2024 21:32
-
-
Save igorbenic/b849890dec93b77785bcf9c7fee7e98c to your computer and use it in GitHub Desktop.
How to Create Dynamic WordPress Email Templates | http://www.ibenic.com/create-dynamic-wordpress-email-templates/
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 | |
function ibenic_email_template_settings() { | |
// Section | |
add_settings_section( | |
'email_templates_section', | |
'Dynamic Email Templates', | |
'ibenic_email_templates_section', | |
'email-templates' | |
); | |
// Field | |
add_settings_field( | |
'email_template_user', | |
'User Registered', | |
'email_template_user_cb', | |
'email-templates', | |
'email_templates_section' | |
); | |
// Value under which we are saving the data by $_POST | |
register_setting( 'email-templates', 'email_template_user' ); | |
} | |
add_action( 'admin_init', 'ibenic_email_template_settings' ); | |
function ibenic_email_templates_section() { | |
echo ''; | |
} | |
function email_template_user_cb() { | |
$content = get_option('email_template_user'); | |
wp_editor( $content, 'email_template_user' ); | |
} |
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 | |
function ibenic_email_template_submenu() { | |
add_submenu_page( | |
'options-general.php', | |
'Email Templates', | |
'Email Templates', | |
'manage_options', | |
'email-templates', | |
'ibenic_email_template_submenu_cb' ); | |
} | |
add_action( 'admin_menu', 'ibenic_email_template_submenu' ); | |
function ibenic_email_template_submenu_cb() { | |
?> | |
<div class="wrap"> | |
<h2><?php _e( 'Email Templates', 'your_textdomain'); ?></h2> | |
<form method="post" action="options.php"> | |
<?php | |
settings_fields( 'email-templates' ); //pass slug name of page, also referred | |
do_settings_sections( 'email-templates' ); //pass slug name of page | |
submit_button(); | |
?> | |
</form> | |
</div> | |
<?php | |
} |
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 | |
add_action( 'user_register', 'ibenic_email_template_on_register', 10, 1 ); | |
function ibenic_email_template_on_register( $user_id ) { | |
// Get User Object | |
$user_info = get_userdata( $user_id ); | |
// Set Dynamic Data with placeholders and values | |
$dynamicData = array( | |
'username' => $user_info->user_login, | |
'useremail' => $user_info->user_email); | |
// Get Admin Email | |
$emails = get_option( 'admin_email' ); | |
// Create Email Subject | |
$subject = 'New User Registered: ' . $user_info->user_login; | |
// Name of our template (Settings Field) | |
$template = 'email_template_user'; | |
// Sending email with our Class | |
new WordPressEmail( $emails, $subject, $dynamicData, $template ); | |
} |
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 | |
class WordPressEmail { | |
/** | |
* Array or String of emails where to send | |
* @var mixed | |
*/ | |
protected $emails; | |
/** | |
* Subject of email | |
* @var string | |
*/ | |
protected $title; | |
/** | |
* Associative Array of dynamic data | |
* @var array | |
*/ | |
protected $dynamicData = array(); | |
/** | |
* Template used to send data | |
* @var string | |
*/ | |
protected $template; | |
/** | |
* Prepared template with real data instead of placeholders | |
* @var string | |
*/ | |
protected $outputTemplate; | |
// ... | |
} |
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 | |
class WordPressEmail { | |
// ... | |
public function __construct($emails, $title, $dynamicData, $template ){ | |
$this->emails = $emails; | |
$this->title = $title; | |
$this->dynamicData = $dynamicData; | |
$this->template = $template; | |
$this->prepareTemplate(); | |
$this->send(); | |
} | |
// ... | |
} |
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 | |
class WordPressEmail { | |
// ... | |
private function prepareTemplate(){ | |
$template = $this->getTemplate(); | |
foreach ($this->dynamicData as $placeholder => $value) { | |
// Ensure that the placeholder will be in uppercase | |
$securePlaceholder = strtoupper( $placeholder ); | |
// Placeholder used in our template | |
$preparedPlaceholder = "{{" . $securePlaceholder . "}}"; | |
// Template with real data | |
$template = str_replace( $preparedPlaceholder, $value, $template ); | |
} | |
$this->outputTemplate = $template; | |
} | |
private function getTemplate(){ | |
return get_option( $this->template ); | |
} | |
private function send(){ | |
wp_mail( $this->emails, $this->title, $this->outputTemplate ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment