There is a post already asking the question here, but it’s from 2024 and was never answered.
The short story is that I have a newsletter signup form that uses a custom action to make sure the user doesn’t already exist, and isn’t already in the “newsletter” role before making the account and/or assigning the role.
If a user is made or added, a welcome email is added using the “email” action. If a user is already signed up, an error is returned to the form using:
else {
$result_output = "This address was already subscribed. Please search your inbox for the original welcome email.";
$result_type = 'error';
}
}
$form->set_result([
'action' => 'my_custom_action',
'type' => "$result_type", // 'success', 'error', or 'info'
'message' => "$result_output",
]);
But… the welcome email still sends. How do I communicate to the form that the custom action has failed and I want no further actions triggered?
short answer: set_result cannot do that. It only adds a message to the response that the visitor sees - it has no influence on which actions run. There is also a second catch: the action order is fixed in the code and the Email action runs before the Custom action, so even if you could abort inside the custom action, the welcome email would already be out.
The place for a check like “is this address already subscribed” is the validate filter. It runs before any action, and if you return errors there the whole submission stops - no user is created, no email is sent, and the visitor gets your message.
add_filter('bricksforge/pro_forms/validate', function ($result, $form_data, $post_id, $form_id) {
// Optional: only this form (the form's data-script-id)
if ($form_id !== 'abc123') {
return $result;
}
// "email" is the Custom ID of your email field
$email = isset($form_data['form-field-email']) ? $form_data['form-field-email'] : '';
if ($email && email_exists($email)) {
return [[
'field' => 'form-field-email',
'message' => 'This address was already subscribed. Please search your inbox for the original welcome email.',
]];
}
return $result;
}, 10, 4);
Return $result unchanged when everything is fine, and an array of errors when it is not. Each error is a field key plus a message; the message is shown on that field, and if the field cannot be matched it is shown as a general form error instead. So you can also use it for your role check - just move that part of your logic from the custom action into the filter.
Keep the custom action for the work that should actually happen (creating the user, adding the role). By the time it runs, validation has already passed.