联系表格7允许在24小时后复制



我使用的联系表格7在我们的网站上获取潜在客户。我还使用CFDB插件添加了一个验证规则,以防止网站上的所有重复使用。

function is_already_submitted($formName, $fieldName, $fieldValue) {
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName=$fieldValue";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = false;
while ($row = $exp->nextRow()) {
$found = true;
}
return $found;
}
function my_validate_email($result, $tag) {
$formName = 'email_form'; // Change to name of the form containing this field
$fieldName = 'email_123'; // Change to your form's unique field name
$errorMessage = 'Email has already been submitted'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName) {
if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
$result->invalidate($tag, $errorMessage);
}
}
return $result;
}

如果用户在24小时后重试,我们现在需要允许重复条目。

我们的建议是运行cron作业来标记超过24小时的条目,然后允许用户继续。我们包含了一个新的表列(allow_duplicate(来标记条目。

如有任何关于如何在functions.php上构建验证的建议,我们将不胜感激。

所以我个人。。。我会使用瞬态API。使用Transients,您可以将过期时间设置为24小时,并消除对cron任务的需要。第一使用钩子wpcf7_before_send_mail设置瞬态,然后使用验证过滤器检查瞬态的存在。过渡期将在首次提交后24小时到期。

在功能dd_handle_form_submission中设置您的联系表格ID

// capture data after contact form submit
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' ); 
function dd_handle_form_submission( $contact_form ) {
// Check Form ID
if( 2744 !== $contact_form->id() ) return; // Use your form ID here
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
// Set the Transient
$transient_name = 'cf7_has_submitted_' . $posted_data['email_123'];
set_transient($transient_name, true, 1 * DAY_IN_SECONDS);
}
return;
}
// Custom Validation Filter
add_filter( 'wpcf7_validate_email*', 'my_validate_email', 20, 2);
function my_validate_email($result, $tag) {
$fieldName = 'email_123'; // Change to your form's unique field name
$errorMessage = 'Email has already been submitted'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName) {
// Create the Transient Name to lookup with the email field
$transient = 'cf7_has_submitted_' . $_POST[$name];
// Lookup transient
if (false !== get_transient($transient)) $result->invalidate($tag, $errorMessage);
}
return $result;
}

最新更新