Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //** Admin Email Alerts for WordPress
- /*
- *
- * First, we must retrieve the 'old' values from the database
- */
- function get_values() {
- global $current_user, $jwl_old_metadata; // Set global variables
- get_currentuserinfo(); // Just as the name suggests
- $jwl_old_metadata = $current_user; // Set a variable equal to current user info
- global $jwl_old_metadata; // Define global variable to be used in function notify_admin_on_update()
- }
- add_action('init','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 notify_admin_on_update(){
- global $current_user, $jwl_old_metadata; // Set global variables
- get_currentuserinfo(); // Just as the name suggests
- // 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
- // If you would like to use it, and comment it out... don't forget the closing bracket below that's also commented out
- // if (!current_user_can( 'administrator' )){// avoid sending emails when admin is updating user profiles
- // 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 Profile Update!'; // Set the 'subject' email field
- // Now we can begin building the email message content
- $message = "<Strong>Hello WordPress Admin,</strong>";
- $message .= "<p>The user, <em><strong>" .$current_user->display_name . "</strong></em>, has updated their profile with the following information:</p><p></p>";
- // Let's set an array of default WP values we DO NOT want to be alerted if they don't match
- $wp_defaults_array = array('_wpnonce','_wp_http_referer','from','checkuser_id','admin_bar_front','action','submit','email','user_id');
- // Iterate through each value that is processed during 'update_profile'.
- foreach ($_POST as $key => $value) {
- // If our value is NOT in the array above, process the data
- if(!in_array($key, $wp_defaults_array)) {
- $constant = $key; // Set constant for comparison sake
- // If the processed post data DOES NOT match the currently stored database value (The user has updated the field)
- if($_POST[$constant] != $jwl_old_metadata->$constant) {
- $message .= "<p><strong>".$key.":</strong><br />";
- $message .= "Old Value: ".$jwl_old_metadata->$constant."<br />";
- $message .= "New Value: ".$_POST[$constant]."</p><p></p>";
- }
- }
- }
- // 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"
- @wp_mail( $to, $subject, $message); // FINALLY, send the darn message!!
- 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.
- // Below is the bracket that should be commented out if you do not wish to receive email alerst when an admin updates a profile.
- //}
- }
- add_action( 'personal_options_update', 'notify_admin_on_update' ); // Hook to user update profile button
- add_action( 'edit_user_profile_update','notify_admin_on_update'); // Hook to edit user update profile button
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement