Created
September 1, 2023 13:26
-
-
Save AWOL-TECH/f6a413754a0e2e62393193b33dd4eb78 to your computer and use it in GitHub Desktop.
WordPress plugin code snippet for forminator that sends email notifications to one or multiple recipients when a form is submitted without email notifications configured. It includes submission details and a link to review the form's email routing configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Author: AWOL-TECH | |
* URL: https://gist.github.com/AWOL-TECH | |
* URL: https://github.com/AWOL-TECH | |
* | |
* Credit: Forminator support | |
* | |
* Function to extend Forminator functionality | |
* | |
* This can be added as an MU Plugin or custom plugin. Or if you must, it should work in functions.php (run a check that forminator exists / is activated first) | |
* for example: if ( class_exists('Forminator')){ | |
* | |
* | |
* if a form is submitted with no To: address defined in the email (routing) notification then send a message to notify of this issue | |
* it will include the form submission ID, the form ID, a link to view the form submissions and the page where the form was submitted (useful for multi form scenarios) | |
* Finally it will give you the submitted form data and a link to review the forms configuration. | |
* | |
* credit to forminator support team for initial help: https://wordpress.org/support/topic/idiot-proofing-the-email-routing-of-a-form/ | |
* | |
* Make sure to change the $recipients array to your chosen email addresses or this won't work. | |
* | |
* ## If you modify or re-use this code please credit me just like I credited The Forminator support team for their initial help. ## | |
* | |
**/ | |
add_filter('forminator_form_get_admin_email_recipients', 'awol_submission_without_notification', 10, 5); | |
function awol_submission_without_notification($email, $notification, $prepared_data, $module, $entry) { | |
// Get the WordPress admin URL | |
$admin_url = admin_url(); | |
// Get the referring URL (the URL of the page where the form was submitted) | |
$referring_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; | |
// Get the form ID and entry ID | |
$form_id = $entry->form_id; | |
$entry_id = $entry->entry_id; | |
// Get the form configuration, including field labels | |
$form = Forminator_API::get_form($form_id); | |
// Check if the $email variable is empty and the form configuration is available | |
if (empty($email) && $form) { | |
// Create the email message with the submission details | |
$message = 'Hi, This submission with ID ' . $entry_id . ' is from Form ID ' . $form_id . ' without email notification.<br />'; | |
$message .= 'View form submission <a href="' . $admin_url . 'admin.php?page=forminator-entries&form_type=forminator_forms&form_id=' . $form_id . '" target="_blank">here</a>.<br />'; | |
// add the url of the page where the form was submitted | |
if (!empty($referring_url)) { | |
$message .= 'The form was submitted from ' . $referring_url . '<br />'; | |
} | |
// Add form submission data to the email message | |
$message .= '<br />Form Submission Data:<br />'; | |
foreach ($prepared_data as $field_key => $field_value) { | |
// Get the field label from the form configuration | |
$field_label = ''; | |
foreach ($form->fields as $field) { | |
if ($field->element_id === $field_key) { | |
$field_label = $field->field_label; | |
break; | |
} | |
} | |
// Only add entries with non-empty field labels | |
if (!empty($field_label)) { | |
$message .= $field_label . ': ' . $field_value . '<br />'; | |
} | |
} | |
// Add a message to review the form's email routing configuration | |
$message .= '<br /><b>Please review the form\'s email routing configuration <a href="' . $admin_url . 'admin.php?page=forminator-cform-wizard&id=' . $form_id . '" target="_blank">here</a>.</b><br />'; | |
// Define an array of recipients with multiple email addresses | |
$recipients = array( | |
'email1@domain.co.uk', // Add your main recipient | |
'email2@domain.co.uk', // Add additional recipients here | |
); | |
// Set the email subject | |
$subject = 'Form submission without email set in notification'; | |
// Set email headers to allow HTML content | |
$headers = array('Content-Type: text/html; charset=UTF-8'); | |
// Send the email with HTML content to multiple recipients | |
wp_mail($recipients, $subject, $message, $headers); | |
} | |
// Always return the $email variable, whether modified or not | |
return $email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment