Challenge With Email On Update

By: Michael Clark | Asked: 07/07/2023
ForumsCategory: How-toChallenge With Email On Update
Michael ClarkMichael Clark asked 1 year ago

I have a code snippet that updates a user's role when an admin clicks a link in a view to change the user's role.

An email gets triggered when the entry is updated. The email also gets triggered when *any* value in the entry is updated. Don't want that. Only want that email to action to fire when the role is updated.

How can I prevent the email action from being triggered by other updates to the entry?

1 Answers
Rob LeVineRob LeVine answered 1 year ago
Can you have the link click action call PHP code that in turn manually triggers the email notification (and turn off the "on update" trigger for the email notification)?
Michael ClarkMichael Clark replied 1 year ago

Hey Rob. That's a *great* idea. Trying to work out in my brains how to trigger that action only when the change_role function is fired. Any ideas you'd care to share? Don't need a static entry ID, the change user's role function and the fire email function are in the same snippet. Thanks for your insights.

Rob LeVineRob LeVine replied 1 year ago

When the admin clicks the link to change the role, what specifically happens, code and functionality-wise.

Rob LeVineRob LeVine replied 1 year ago

When the admin clicks the link to change the role, what specifically happens, code and functionality-wise?

Michael ClarkMichael Clark replied 1 year ago

Here's the snippet:

function update_applicant_role( $userid, $role ) {
$user = get_userdata( $userid );
if ( $user && ! $user->has_cap('administrator') ) {
$user->set_role( $role );
}
}

add_action('frm_after_update_entry', 'update_to_standard', 10, 2);
function update_to_standard$entry_id, $form_id){
if ( $form_id == 73 ) {

$userid = $_POST['item_meta'][2121];// ID of the userID field
$role = $_POST['item_meta'][2122];// ID of the role field

if ( $userid && $role ) {
update_applicant_role( $userid, $role );
}
}
}

add_action('frm_after_update_field', 'frm_trigger_update_entry');
function frm_trigger_update_entry($atts){
$userid_field= '2121';
$entry = FrmEntry::getOne( $atts['entry_id'], true );
$userid = FrmEntryMeta::get_meta_value( $entry, $userid_field );
$role = $atts['value'];
if ( $userid && $role ) {
update_applicant_role( $userid, $role );
}
$form = FrmForm::getOne( $entry->form_id );

FrmFormActionsController::trigger_actions('update', $form, $entry);

Rob LeVineRob LeVine replied 1 year ago

It seems like the answer is right in front of you. Instead of triggering the update in the last line, use the tutorial I referred to above and trigger an email action. You may want to set up a separate email action for this purpose and you can leave it as disabled so it'll only be used in this case.

Michael ClarkMichael Clark replied 1 year ago

Hey Rob. Thanks for that. While the answer may indeed be staring me in the face, I'm not proficient enough to take the info offered in the tutorial and make it work for me. The tutorial could probably serve a decent coder very well. For instance, ideally, the entry id part should be dynamic. I have no idea how to do that and I don't even know where to look. If I did, I'd not know how to code the function or functions to make it go. Still at a loss. Thanks for the insights, though. Greatly appreciated. If you have a more complete example or maybe a link to something in the documentation (I'm hit and miss when it comes to finding stuff that others easily land on in the doc) that'd be awesome.

Rob LeVineRob LeVine replied 1 year ago

You already have access to the entry id in the cost you posted - $atts['entry_id'] as well as $form. Other than all you need to do is get the action id of the action you want to happen (see the comments in my code). Also, I said previously, you may want to create a new notification for this purpose. Then you replace FrmFormActionsController::trigger_actions('update', $form, $entry); in your code with FrmNotification::trigger_email( $actionToDo, $entry, $form );

Michael ClarkMichael Clark replied 1 year ago

Thanks, Rob! I'll hack around and see if I can get it to work.

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