You’ve been struggling with users submitting the same email address multiple times in your Elementor Pro forms, and your search for a solution brought you to this blog post. This post introduces a code snippet that directly addresses this issue, ensuring accurate data and preventing misuse.
The code is built using Object-Oriented Programming principles, and its MuktoEmailRestrictionHandler class offers a reliable and optimized way to block duplicate email submissions.
Read on to learn how this user-friendly code can easily integrate with your WordPress site, providing a dependable solution to this common problem.
Step 1
Go to your Elementor editor panel on the page where you have the form. Then in the Form Name field, enter something unique like an ID. For example, I entered testfrom.
Step 2
Place this code in your child theme’s functions.php file, and change the form name. If you have multiple forms, you can add them to the array as well.
/**
* Class MuktoEmailRestrictionHandler
*
* This class is responsible for restricting form submissions based on the email address used in Elementor Pro forms.
* It prevents users from submitting the same email address multiple times for a specific form.
*
* This code is posted on https://code.mukto.info
*/
class MuktoEmailRestrictionHandler {
private $wpdb;
private $table_prefix;
private $target_form_names;
private $submissions_table;
private $values_table;
/**
* MuktoEmailRestrictionHandler constructor.
*
* @param array $target_form_names An array of form names for which the email restriction should be applied.
*/
public function __construct($target_form_names) {
global $wpdb;
$this->wpdb = $wpdb;
$this->table_prefix = $wpdb->prefix;
$this->target_form_names = $target_form_names;
$this->submissions_table = $this->table_prefix . 'e_submissions';
$this->values_table = $this->table_prefix . 'e_submissions_values';
// Register the validate_email method as a callback for the elementor_pro/forms/validation/email action hook.
add_action('elementor_pro/forms/validation/email', array($this, 'validate_email'), 10, 3);
}
/**
* Validate the email address being submitted.
*
* @param array $field The email field data.
* @param \ElementorPro\Modules\Forms\Records\Record $record The form record object.
* @param \ElementorPro\Modules\Forms\Submissions\Ajax_Handler $ajax_handler The AJAX handler object.
*/
public function validate_email($field, $record, $ajax_handler) {
$form_name = $record->get_form_settings('form_name');
// Check if the current form is one of the target forms.
if (!in_array($form_name, $this->target_form_names)) {
return;
}
$invalid_emails = $this->get_submitted_emails($record->get_form_settings('id'), $form_name);
// Check if the email address being submitted is already present in the list of invalid emails.
if (in_array($field['value'], $invalid_emails)) {
$ajax_handler->add_error($field['id'], "You have already submitted with this email!");
}
}
/**
* Retrieve the email addresses that have already been submitted for a specific form.
*
* @param int $form_id The form ID.
* @param string $form_name The name of the form.
* @return array An array of email addresses that have already been submitted for the given form.
*/
private function get_submitted_emails($form_id, $form_name) {
$form_submissions = $this->wpdb->get_results(
$this->wpdb->prepare(
"SELECT id FROM {$this->submissions_table} WHERE form_name = %s",
$form_name
)
);
$form_submission_ids = array_column($form_submissions, 'id');
if (empty($form_submission_ids)) {
return array();
}
$placeholders = rtrim(str_repeat('%d,', count($form_submission_ids)), ',');
$results = $this->wpdb->get_results(
$this->wpdb->prepare(
"SELECT s.value AS email
FROM {$this->values_table} AS s
INNER JOIN (
SELECT submission_id, MAX(id) AS max_id
FROM {$this->values_table}
WHERE `key` = 'email'
GROUP BY submission_id
) AS e ON s.submission_id = e.submission_id AND s.id = e.max_id
WHERE s.submission_id IN ($placeholders)",
$form_submission_ids
)
);
return array_column($results, 'email');
}
}
// Usage
// Create an instance of the MuktoEmailRestrictionHandler class and pass the array of target form names as an argument.
$email_restriction_handler = new MuktoEmailRestrictionHandler(array('testfrom'));
Here, the MuktoEmailRestrictionHandler(array('testfrom'));
line specifies ‘testfrom‘ as an example of a form name. If you have multiple forms, the code will look something like this.
$email_restriction_handler = new MuktoEmailRestrictionHandler(array('testfrom', 'testfrom2'));
That’s all. Now, if you try to submit your form with the same email, you will receive an error message like this.
If you want to change the error message you can change it here:
if (in_array($field['value'], $invalid_emails)) {
$ajax_handler->add_error($field['id'], "You have already submitted with this email!");
}
I hope this solution is helpful for you. Feel free to reach out if you encounter any issues. If you require further assistance or would like to explore paid support options, please don’t hesitate to leave a comment or contact me directly.
Happy Coding 😊