Skip to content

Instantly share code, notes, and snippets.

@bi0xid
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bi0xid/cd1896f7be881d02398f to your computer and use it in GitHub Desktop.
Save bi0xid/cd1896f7be881d02398f to your computer and use it in GitHub Desktop.
WangGuard - Admin Email Alerts for WordPress
<?php
//** Admin Email Alerts for WordPress
// Original @ Josh Lobe - WP Admin Alerts For Profile Updates
// http://pastebin.com/XY4bgiZX
// WangGuard version @ Rafa Poveda
// https://gist.github.com/bi0xid/cd1896f7be881d02398f
/*
* First, we must retrieve the 'old' values from the database
*/
function wangguard_get_values() {
global $current_user, $wg_old_user; // Set global variables
get_currentuserinfo();
$wg_old_user = $current_user; // Set a variable equal to current user info
global $wg_old_user; // Define global variable to be used in function notify_admin_on_update()
}
add_action('init','wangguard_get_values'); // Add to init so we know our values are process BEFORE 'update_profile' hook
/*
* Now we can process the 'update_profile' hook data and compare values
*/
function wangguard_notify_admin_on_update(){
global $current_user, $wg_old_user; // Set global variables
get_currentuserinfo();
$send_mail = false; // We are sending a mail if the current mail is changing
// Set email 'to' and 'subject' fields
$to = get_option( 'admin_email' ); // Get the admin email address, and set it to the 'to' email field
$subject = __( 'WordPress Admin Notification - User Email Update!' ); // Set the 'subject' email field
// Now we can begin building the email message content
$message = sprintf( __( '<strong>Hello WordPress Admin,</strong>
<p>The user <em><strong>%s</strong></em> has updated their email information:</p><p></p>' ), $current_user->display_name );
// If the processed post data DOES NOT match the currently stored database value (The user has updated the field)
if($_POST['email'] != $wg_old_user->user_email) {
$message .= sprintf( __( '<p><strong>The e-mail has changed:</strong></p>
<p>Old Value: %s<br />
New Value: %s</p>'), $wg_old_user->user_email, $_POST['email'] );
$send_mail = true; //send the email
}
// Let's build a function for setting email type to html
function set_html_content_type() {
return 'text/html';
}
add_filter( 'wp_mail_content_type', 'set_html_content_type' ); // Let's set WP mail to html.. so we can make the email look "pretty"
if ( $send_mail == true )
@wp_mail( $to, $subject, $message); // Send the mail if neccessary
remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); // Don't forget to remove the html filter, so as not to interfere with other plugins/themes.
}
add_action( 'personal_options_update', 'wangguard_notify_admin_on_update' ); // Hook to user update profile button
add_action( 'edit_user_profile_update','wangguard_notify_admin_on_update'); // Hook to edit user update profile button
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment