Capturing user's name when entry is edited to use in email notification to other users

By: Isaac Golle | Asked: 07/26/2024
ForumsCategory: Code HelpCapturing user's name when entry is edited to use in email notification to other users
Isaac Golle asked 5 months ago

I've created an app that has 'editor' wordpress users creating quotes that need to be approved by 'admin' wordpress users.  Workflow looks like:

  1. 'Editor' creates quote entry
  2. 'Admin' users get notified via email to review/approve
  3. 'Admin' users edit the original entry by changing a field called 'status' to 'send to client'(only admin users can see this field).

What I'd like to achieve is to have admin users be notified when other admin users make changes to an entry and include the name of the user that made the change.  I know I can send a general email notification to these users whenever changes are made, but it can be helpful to them to know who made the changes in case follow up is needed. I thought I could achieve this by using the User ID field, but it appears that the user ID field does not update from the original entry.  This has resulted in admin users being notified that editor level users are making changes they shouldn't be allowed to make because the email notification is just using the static original User ID on an entry. I contacted Formidable support and they pointed me in the direction of this hook but I'm unclear where to go from here.  I think what I need to do is create a hidden field which is designed to capture the user ID of the admin user making edits(would I just set a dynamic default value?) and then include that in the email notification(s) to other admins.  But this is all front end facing and the hook is saying that I need to set a specific value for a front end use case. I'm a bit confused!  xml of the application is included and the specific form in question is 'quote builder'

Attachments
1 Answers
Best Answer
Rob LeVineRob LeVine answered 5 months ago
I assume Strategy 11 was suggesting you use frm_setup_edit_fields_vars to fill in a hidden with the current user's id. That way, when the email is sent, you can use that value to demonstrate who was the last person to change it. I wouldn't let the semantics bog you down. Create a hidden field and let's say its ID is 999. In the code you have the if checking against 999 and then inside the if you set $values['value'] = get_current_user_id() or user $current_user = wp_get_current_user(); and set the value to $current_user->display_name
Isaac Golle replied 5 months ago

Ok I think this makes sense. Do I need to set a default value in the hidden field or just leave it blank and the PHP is dealing with it?

Rob LeVineRob LeVine replied 5 months ago

The default value will only work on a new entry. If you want a default on a new entry, you'll have to use https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/ and do the same thing in the previous example. Given what you described, that's not something you want to do.

Isaac Golle replied 5 months ago

Got it. So then if I use the hidden field in email notifications it will always use the updated value of that field?

Rob LeVineRob LeVine replied 5 months ago

Yes, though the best way to prove that is to try it.

Isaac Golle replied 5 months ago

So I put this in and it's returning the correct user ID on edits now, but displaying the user ID instead of user first name in email notifications. Any idea where I'm going wrong?

add_filter('frm_setup_edit_fields_vars', 'frm_set_edit_val', 20, 3);
function frm_set_edit_val( $values, $field, $entry_id ) {
if ( $field->id == 138 ) { // Replace 138 with your hidden field ID
// If on the back-end, don't adjust field value
if ( FrmAppHelper::is_admin() ) {
return $values;
}

// If on the front-end, set the hidden field value
$current_user_id = get_current_user_id();
$values['value'] = $current_user_id;

// Now retrieve the user's first name
$user_info = get_userdata($current_user_id);
$user_first_name = $user_info->first_name;
}
return $values;
}

Rob LeVineRob LeVine replied 5 months ago

The code is not storing the name in $values, it's storing the ID. In fact it doesn't do anything at all with the name. If all you want is the name, just store it and forget about the userid. If you need the id because you want the complete information on the user, then store it $values and create a custom shortcode that takes the userid as a parameter and gets whatever user information you need for the email notification.

Isaac Golle replied 5 months ago

Follow up: I used the below code and it is working perfectly.

add_filter('frm_setup_edit_fields_vars', 'frm_set_edit_val', 20, 3);
function frm_set_edit_val( $values, $field, $entry_id ) {
if ( $field->id == 138 ) { // Replace 138 with your hidden field ID
// Retrieve the user's first name
$current_user_id = get_current_user_id();
$user_info = get_userdata($current_user_id);
$user_first_name = $user_info->first_name;

// Set the hidden field value to the user's first name
$values['value'] = $user_first_name;
}
return $values;
}

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right