Custom Contact Form: $wpdb->insert running twice?
-
I created my own contact form plugin years ago and have used it on several WP sites. To prevent multiple submissions, a random, unique stamp is generated and inserted into a database table field that only allows unique values. This form works fine on sites running WP 4.8 or 4.9 and earlier, but on sites running 4.9 or 5.0 plus, it generates a duplicate entry error on the very first submission. The email is still sent and duplicate submissions are still prevented, but it seems like the insert routine is running twice in the newer versions of WP and I can’t figure out why.
The stamps table is created with the following code upon plugin activation:
$charset_collate = $wpdb->get_charset_collate(); $table_name = $wpdb->prefix."cf_stamps"; $sql = "CREATE TABLE IF NOT EXISTS $table_name ( entry_id int(10) NOT NULL AUTO_INCREMENT, entry_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, entry_stamp varchar(32) NOT NULL, PRIMARY KEY (entry_id), UNIQUE KEY entry_stamp (entry_stamp) ) $charset_collate;";
The contact form is inserted into a WP page via a shortcode:
function contact_form_shortcode($atts) { ob_start(); $a = shortcode_atts( array( 'language' => 'en' ), $atts ); $lang = $a['language']; contact_form_process($lang);// Processes the contact form after submission contact_form_email($lang);// Checks for errors and emails form data if error free contact_form_markup($lang);// Generates contact form markup return ob_get_clean(); } add_shortcode('contact_form', 'contact_form_shortcode');
After submission and processing, form data checked for errors and emailed. Unique stamp is inserted into the database table upon first submission.:
function contact_form_email($lang='en') { global $showForm;// Form is only displayed if there are errors that need correcting or an email error has occurred global $cf_form_errors, $cf_user_errors, $cf_form_missing;// Form submission and user errors global $cf_email_main, $cf_emails_bc, $cf_emails_cc;// Email addresses to which to send contact form data global $wpdb; $cf_stamps_table = $wpdb->prefix.'cf_stamps';// Database table for stamps (to prevent multiple submissions) $userErrorMsgs = cf_get_user_error_msgs(); if (isset($_POST['submitForm'])) { if ($cf_form_errors) {// True if honeypot has been filled, cf_stamp empty or invalid, etc. echo "\n\t\t\t", '<p class="align-center error">This form has been accessed in error.</p>', "\n"; $_POST = array(); $showForm = FALSE; } elseif ($cf_form_missing || $cf_user_errors) {// True if any required fields are missing or any fields contain invalid data echo "\n\t\t\t", '<p class="align-center error">Please address the errors indicated below and resubmit your data.</p>', "\n"; $showForm = TRUE; } else {// No errors, proceed with sending form data via email $cf_stamp = $_POST['cf_stamp'];// Submitted via hidden field, value=md5(uniqid(rand(), true)) $wpdb->hide_errors(); if ($wpdb->insert($cf_stamps_table, array('entry_stamp' => $cf_stamp))) { if (cf_contact_form_send_email($cf_email_main, $cf_emails_bc, $cf_emails_cc, $lang)) {// Composes email and sends to main, BC, CC using wp_mail() echo "\n\t\t\t", '<p class="contact-thank">Thank you for contacting us. Your information has been submitted successfully. We will reply as soon as possible.</p>', "\n"; $showForm = FALSE; } else {// Email did not send successfuly echo "\n\t\t\t", '<p class="align-center error">A system error has occurred. We aplogize for the inconvenience. Please try again later.</p>', "\n"; $showForm = TRUE; } } else {// Form submitted multiple times (stamp already in database table) echo "\n\t\t\t", '<p class="contact-thank">The form has already been submitted and processed. Thank you.</p>', "\n"; $showForm = FALSE; } } } else { return; } }
In older WP versions, I see the “Thank you for contacting us. Your information has been submitted successfully….” message upon first submission. In newer WP versions, I immediately see the “The form has already been submitted and processed….” message and my error log shows a duplicate entry error, but the email is still sent. So it seems like insert is running twice in newer WP versions. Can anyone take a guess as to why this is happening?
This isn’t the end of the world since the form is still fulfilling its main function, but it’s driving me nuts and I’d really appreciate any advice on where I can look for a solution.
- The topic ‘Custom Contact Form: $wpdb->insert running twice?’ is closed to new replies.