【问题标题】:contact form 7 "before_mail_send" stop from sending and show custom message instead联系表格 7“before_mail_send”停止发送并显示自定义消息
【发布时间】:2020-10-10 19:21:34
【问题描述】:

我使用联系表单 7“before_send_mail”功能检查表单字段,以及字段“密码”内的值是否与预定义的密码匹配。如果字段“密码”中的值等于有价值的 $definedPassword 表单应该正常执行但是当值不同时我不想执行表单并显示自定义消息。我很高兴得到每一个帮助:

<?php 
    add_action( 'wpcf7_before_send_mail', 'wpcf7_login_stream_check' );

    function wpcf7_login_stream_check($WPCF7_ContactForm) {
        $definedPassword = "234";
        $wpcf7 = WPCF7_ContactForm :: get_current();
        $form_id = $wpcf7->id;
        
        if ($form_id === 105) {
            $submission = WPCF7_Submission::get_instance();  
            if ($submission) {
                $posted_data = $submission->get_posted_data();
                $password = $posted_data['password'];
        
                if($password == $definedPassword ) {
                    /*  NORMAL BEHAVIOUR */
                } else {
                    /* PREVENT FORM FROM SENDING AND SHOW CUSTOM-MESSAGE */
                }
            }
        }
    }

【问题讨论】:

  • 除非您需要运行 sql 查询来确定数据库中的密码是否有效,否则我会尝试使用 JS 而不是 php 来解决此问题。如果你决定使用 php,你需要检查表单值是否等于你的 $definedpassword 使用 ISSET

标签: php wordpress contact-form-7


【解决方案1】:

我认为最好不要使用before_send_mail,而是使用表单验证功能。如果您的字段是文本字段,并且密码是静态的...如果您添加到您的 functions.php,这将起作用。如果您不使用[text* password],您可以在add_filter 中相应地调整标签类型

使用 Contact Form 7 5.2.2 测试

function cf7_check_password_field($result, $tags) {
    $definedPassword = "234";
    
    $tags = new WPCF7_FormTag( $tags );

    $name = $tags->name;
    
    // Check the tag name is password or exit
    if ($name !== 'password') return $result;
    // if the password isnt valid
    if ($_POST['password'] !== $definedPassword){
        // set your message here
        $result->invalidate( $tags, __( 'Password does not match.', 'text_domain' ));

    }
    return $result;
}

add_filter('wpcf7_validate_text', 'cf7_check_password_field', 20, 2); // text field
add_filter('wpcf7_validate_text*', 'cf7_check_password_field', 20, 2); // Req. text field

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多