daily pastebin goal
46%
SHARE
TWEET

admin email code - Josh

a guest Aug 9th, 2013 213 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //** Admin Email Alerts for WordPress
  3.  
  4. /*
  5.  *
  6.  * First, we must retrieve the 'old' values from the database
  7.  */
  8. function get_values() {
  9.        
  10.         global $current_user, $jwl_old_metadata;  // Set global variables
  11.     get_currentuserinfo();  // Just as the name suggests
  12.         $jwl_old_metadata = $current_user;  // Set a variable equal to current user info
  13.         global $jwl_old_metadata;  // Define global variable to be used in function notify_admin_on_update()
  14. }
  15. add_action('init','get_values');  // Add to init so we know our values are process BEFORE 'update_profile' hook
  16.        
  17. /*
  18.  *
  19.  * Now we can process the 'update_profile' hook data and compare values
  20.  */
  21. function notify_admin_on_update(){
  22.        
  23.     global $current_user, $jwl_old_metadata;  // Set global variables
  24.     get_currentuserinfo();  // Just as the name suggests
  25.        
  26.         // The following is an 'if' statement.  It can be commented out if you would like to NOT receive email alerts when admins are updating profiles
  27.         // If you would like to use it, and comment it out... don't forget the closing bracket below that's also commented out
  28.        
  29.     // if (!current_user_can( 'administrator' )){// avoid sending emails when admin is updating user profiles
  30.        
  31.                 // Set email 'to' and 'subject' fields
  32.         $to = get_option('admin_email');  // Get the admin email address, and set it to the 'to' email field
  33.         $subject = 'WordPress Admin Notification - User Profile Update!';  // Set the 'subject' email field
  34.                
  35.                 // Now we can begin building the email message content
  36.                 $message = "<Strong>Hello WordPress Admin,</strong>";
  37.         $message .= "<p>The user, <em><strong>" .$current_user->display_name . "</strong></em>, has updated their profile with the following information:</p><p></p>";
  38.                
  39.                 // Let's set an array of default WP values we DO NOT want to be alerted if they don't match
  40.                 $wp_defaults_array = array('_wpnonce','_wp_http_referer','from','checkuser_id','admin_bar_front','action','submit','email','user_id');
  41.                
  42.                 // Iterate through each value that is processed during 'update_profile'.
  43.                 foreach ($_POST as $key => $value) {
  44.                         // If our value is NOT in the array above, process the data
  45.                         if(!in_array($key, $wp_defaults_array)) {
  46.                                 $constant = $key;  // Set constant for comparison sake
  47.                                
  48.                                 // If the processed post data DOES NOT match the currently stored database value (The user has updated the field)
  49.                                 if($_POST[$constant] != $jwl_old_metadata->$constant) {
  50.                                         $message .= "<p><strong>".$key.":</strong><br />";
  51.                                         $message .= "Old Value: ".$jwl_old_metadata->$constant."<br />";
  52.                                         $message .= "New Value: ".$_POST[$constant]."</p><p></p>";
  53.                                 }
  54.                         }
  55.                 }
  56.                
  57.                 // Let's build a function for setting email type to html
  58.                 function set_html_content_type() {
  59.                         return 'text/html';
  60.                 }
  61.                
  62.                 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"
  63.         @wp_mail( $to, $subject, $message);  // FINALLY, send the darn message!!
  64.                 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.
  65.        
  66.         // Below is the bracket that should be commented out if you do not wish to receive email alerst when an admin updates a profile.
  67.                
  68.     //}
  69. }      
  70. add_action( 'personal_options_update', 'notify_admin_on_update' );  // Hook to user update profile button
  71. add_action( 'edit_user_profile_update','notify_admin_on_update');  // Hook to edit user update profile button
  72.  
  73.  
  74. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top