Skip to content

Instantly share code, notes, and snippets.

@ckpicker
Last active March 29, 2020 22:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckpicker/0e21f547d817c9ba56c3 to your computer and use it in GitHub Desktop.
Save ckpicker/0e21f547d817c9ba56c3 to your computer and use it in GitHub Desktop.
Pods Changed Fields Email Notification
/*
This function will send an admin email notification with the field values that have changed on a Pods->form()
*/
add_filter('pods_api_pre_save_pod_item', 'my_pre_save_function', 10, 2);
function my_pre_save_function($pieces, $is_new_item) {
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
//Check to see if request was submitted from a specific front-end WordPress page
if($_POST[_podsfix__pods_location] == "/members/manage-your-profile/?success=1") {
//Fields we want to check against
$fields = array(
'name',
'last_name',
'preferred_name',
'suffix',
'title',
'address_1',
'city',
'state',
'zip',
'email',
'display_email',
'phone',
'mobile_phone',
'display_mobile',
'firm',
'display_map',
'photo',
'section',
'member_id',
'spouse_first_name',
'spouse_last_name',
'membership_status',
'membership_type'
);
$member_pod = pods( 'members', $pieces['params']->id );
$changed_fields = array();
$i = 0;
//Loop through fields and check if submitted data is different from the DB
//Save Old/New Value pairs in $changed_fields if different
foreach($fields as $single_field) {
//Check if submitted data is different from current data
if($member_pod->field($single_field) != $_POST['_podsfix_pods_field_'.$single_field]) {
$changed_fields[$i]['Label'] = $member_pod->fields( $single_field, 'label' );
$changed_fields[$i]['New Value'] = $_POST['_podsfix_pods_field_'.$single_field];
$changed_fields[$i]['Old Value'] = $member_pod->field($single_field);
$i++;
}
}
//Build HTML String for Email
$html_string = "<h2>Information Updated for ". $member_pod->field('name') ." ". $member_pod->field('last_name') ." (Member #".$member_pod->field('member_id').")</h2>";
//Loop through $changed_fields and add to Email Message String
foreach($changed_fields as $changed_field) {
$html_string .= '<p><strong>'.$changed_field['Label'].'</strong></p>';
$html_string .= '<ul><li>Old Value: '.$changed_field['Old Value'].'</li>';
$html_string .= '<li>New Value: '.$changed_field['New Value'].'</li></ul>';
}
//Send Email
$admin_email = get_option('admin_email');
wp_mail( $admin_email, 'Member Updated', $html_string );
}
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
}
function set_html_content_type() {
return 'text/html';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment