繁体   English   中英

检查我的数据库中是否存在通过联系表 7 提交的 email?

[英]check if email submitted via Contact form 7 exists in my database?

当客户通过联系表 7 提交 email 时,我如何检查我的数据库中是否已存在 email 并将通知消息更改为“您的 email 已存在于我们的数据库中”

我尝试了两个代码,但不起作用

add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );

function email_already_in_db ( $result, $tags ) {
    // retrieve the posted email
    $form  = WPCF7_Submission::get_instance();
    $email = $form->get_posted_data('your-email');
    // if already in database, invalidate
    if( email_exists( $email ) ) // email_exists is a WP function
        $result->invalidate('your-email', 'Your email exists in our database');
    // return the filtered value
    return $result;
}

这第二个选项也不起作用,

function email_already_in_db ( $result, $tags ) {
    // Retrieve the posted form
    $form  = WPCF7_Submission::get_instance();
    $form_posted_data = $form->get_posted_data();

    // Get the field name that we want to check for duplicates.
    // I added 'unique' to the beginning of the field name in CF7
    // Checking for that with preg_grep
    $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));

    // $unique_field_name comes back as array so the next three lines give us the key as a string
    reset($unique_field_name);
    $first_key = key($unique_field_name);
    $unique_field_name = $unique_field_name[$first_key];

    // Check the form submission unique field vs what is already in the database
    $email = $form->get_posted_data($unique_field_name);
    global $wpdb;
    $entry = $wpdb->get_results( "SELECT * FROM wp_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );

    // If already in database, invalidate
    if (!empty($entry)) {
      $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
      }
    // return the filtered value
  return $result;
}

有帮助吗? 谢谢

最好的办法是为表单中的 email 字段指定一个唯一的名称。 仅在此表单上使用此名称,不要使用其他名称。 在此示例中,我们使用“email_123”。 此示例的 Contact Form 7 表单定义为:表单名称:email_form

<p>Your Name (required)<br />
    [text* name] </p>
 
<p>Your Email (required)<br />
    [email* email_123] </p>
 
<p>[submit "Send"]</p>

要创建验证,我们使用 Shortcodes Actions and Filters 插件将 WordPress 过滤器代码添加到 Tools -> Shortcodes Actions and Filters 中。 这与在保存之前更改表单数据中使用的技术相同。

下面的代码是一个例子。 您需要进行一些更改才能使其适合您。 在 my_validate_email function 中:

将 $formName 更改为您的表单名称 将 $fieldName 更改为您的 email 字段的名称 将 $errorMessage 更改为您喜欢的错误消息

/**
 * @param $formName string
 * @param $fieldName string
 * @param $fieldValue string
 * @return bool
 */
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;
}
 
/**
 * @param $result WPCF7_Validation
 * @param $tag array
 * @return WPCF7_Validation
 */
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;
}
 
// use the next line if your field is a **required email** field on your form
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
// use the next line if your field is an **email** field not required on your form
add_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);
 
// use the next line if your field is a **required text** field
add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);
// use the next line if your field is a **text** field field not required on your form 
add_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM