Guest User

Josh Lobe - WP Admin Alerts For Profile Updates

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