I've created an app that has 'editor' wordpress users creating quotes that need to be approved by 'admin' wordpress users. Workflow looks like:
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'
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?
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.
Got it. So then if I use the hidden field in email notifications it will always use the updated value of that field?
Yes, though the best way to prove that is to try it.
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;
}
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.
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;
}
Please login or Register to submit your answer