Php WordPress联系人表单7文本值更改

Php WordPress联系人表单7文本值更改,php,wordpress,contact-form-7,Php,Wordpress,Contact Form 7,WordPress-联系方式7 我试图找出当有人在其中输入值时修改cf7字段值的过滤器 当用户在文本字段中键入并提交数据时 验证-我已经做了 如果输入无效,不应转到“谢谢”页面-我已完成 用新数据替换文本字段-未完成 例:1 add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func_tel', 100, 2 ); function your_validation_filter_func_tel( $result, $tag

WordPress-联系方式7

我试图找出当有人在其中输入值时修改cf7字段值的过滤器

当用户在文本字段中键入并提交数据时

  • 验证-我已经做了
  • 如果输入无效,不应转到“谢谢”页面-我已完成
  • 用新数据替换文本字段-未完成
  • 例:1

    add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func_tel', 100, 2 );
    function your_validation_filter_func_tel( $result, $tag ) {
    
        $Yourvalue = $_POST['your-number'];
        if ( strlen( $Yourvalue ) == 2 ) {
            $result->invalidate( 'your-number', "Please enter a valid number.  " . );
            // HERE I WANT TO REPLACE NEW DATA TO TEXT FIELD
            $result->data( 'your-number', '1002' );
        } else if ( strlen( $Yourvalue ) == 3 ) {
            $result->invalidate( 'your-number', "Please enter a valid name." . );
            // HERE I WANT TO REPLACE NEW DATA TO TEXT FIELD
            $result->data( 'your-number', '1003' );
        }
    
        return $result;
    }
    
    例:2

    另一个有效的例子

    除了
    $result['tel']=$tel\u cleaned\u final之外,一切正常

        <?php
    
        function custom_filter_wpcf7_is_tel( $result, $tel ) 
        {
    
            // Initialization and Clean Input
            $tel_cleaned         = strtr( $tel, array(' '=>'', '-'=>'', '.'=>''));
            $tel_cleaned_trimmed = ltrim(strtr( $tel_cleaned, array('+'=>'')), '0');
    
    
            /* Test Conditions.
               I concluded 3 if conditions to 1 below bcaz the validation is working perfect
            */
            if ('test conditions here')
            $tel_cleaned_final = substr($tel_cleaned_trimmed, 2);
            else
            $tel_cleaned_final = $tel_cleaned_trimmed;
    
    
    
            if (strlen($tel_cleaned_final) == 10)
            {
            $result = true;
    
            //$result['tel'] = $tel_cleaned_final; 
            /* 
            Here i want to return new number to text box
            for eg: +91 98989-89898 returns  9898989898
            */
    
            }
            else
            {
            $result = false;
            }
    
            return $result;
        }
        add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );
    
        ?>
    
    
    
    也许这有助于:

    add_action( 'wpcf7_before_send_mail', 'some_function_name', 1 ); 
    function some_function_name( $contact_form ) {
        $wpcf7 = WPCF7_ContactForm::get_current();
        $submission = WPCF7_Submission::get_instance();
    
        if ($submission) {
            $data = array();
            $data['posted_data'] = $submission->get_posted_data();
            $firstName = $data['posted_data']['first-name']; // just enter the field name here
            $mail = $wpcf7->prop('mail');
    
            if($firstName =''){
                $mail['body'] = str_replace('[first-name]', $firstName . '-blah blah', $mail['body']);
            }
    
            $wpcf7->set_properties(array(
                "mail" => $mail
            )); 
    
            return $wpcf7;
        }
    }
    
    希望有帮助


    注意:这没有经过测试,请让我知道它是否有效:)

    仅使用验证过滤器无法完成您试图执行的操作。因为它只是根据执行的验证输出true或false。要执行所需操作,必须使用另一个过滤器(“wpcf7\u posted\u data”),该过滤器具有要过滤的值。因此,我们可以将我们的过程分为两个步骤

    第1步:像当前一样执行所有验证

    使用您的示例2

    function custom_filter_wpcf7_is_tel( $result, $tel )
    {
        // Initialization and Clean Input
        $tel_cleaned         = strtr( $tel, array(' '=>'', '-'=>'', '.'=>''));
        $tel_cleaned_trimmed = ltrim(strtr( $tel_cleaned, array('+'=>'')), '0');
    
        /* Test Conditions.
           I concluded 3 if conditions to 1 below bcaz the validation is working perfect
        */
        if ('test conditions here')
        $tel_cleaned_final = substr($tel_cleaned_trimmed, 2);
        else
        $tel_cleaned_final = $tel_cleaned_trimmed;
    
        if (strlen($tel_cleaned_final) == 10)
        {
            $result = true;
        } else
        {
            $result = false;
        }
    
        return $result;
    }
    add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );
    
    以上代码将确保第1点和第2点有效

  • 验证
  • 如果条目无效,请停止提交
  • 步骤2:重新运行测试以获得所需的值并进行更新

    function sr_change_updated_field_value( $posted_data ) 
    { 
        // Initialization and Clean Input
        $tel_cleaned         = strtr( $tel, array(' '=>'', '-'=>'', '.'=>''));
        $tel_cleaned_trimmed = ltrim(strtr( $tel_cleaned, array('+'=>'')), '0');
    
        /* Test Conditions.
           I concluded 3 if conditions to 1 below bcaz the validation is working perfect
        */
        if ('test conditions here')
        $tel_cleaned_final = substr($tel_cleaned_trimmed, 2);
        else
        $tel_cleaned_final = $tel_cleaned_trimmed;
    
        // Use the name of your field in place of "tel"
        $posted_data['tel'] = $tel_cleaned_final;
    
        return $posted_data;
    }; 
    add_filter( 'wpcf7_posted_data', 'sr_change_updated_field_value', 10, 1 );
    
    p.S.这将更新电子邮件中发送的值,如果您存储了这些值,则会更新提交的值。它将显示验证消息,但不会在文本字段中显示更新后的值,因为在这种情况下,php无法完成此操作


    p.S.2这是所有经过测试的代码。快乐编码。

    你的问题很广泛。我建议看一下@janw,我想要的是非常简单的,用实例名来更改表单value@J.K您能分享一下您的联系表单代码吗?@MukeshPanchal请参阅示例:2联系表单7的操作挂钩不允许更改输入字段。您必须添加一个在出错时触发的DOM事件,然后根据需要通过JavaScript检查并更新DOM中的字段。请看,这在CF7 v 5.0.5中似乎不起作用。如果我放一个“die();”在函数名之后并提交表单,die()就不会发生。有什么想法吗?你能更新插件吗?目前的版本是7.5.1.1,尽管我不认为这些过滤器是最近添加的。我无法让版本5.0.5在上面进行测试。根据WP repository页面,联系人表单7位于版本5.1.1。我还没有升级到5.1x,因为它如何强制使用reCAPTCHA v3,以及这对整个站点的加载时间和reCAPTCHA加载的影响(另外,请参阅他们的支持论坛,了解大量关于垃圾邮件增加的投诉)。啊。我的错。关于这个版本,你是对的。我误解了它。但我记得在发布此解决方案时对其进行了测试。但是我没有时间和资源来测试这个自动取款机。不过,我肯定可以指导你做什么。它发生了,但您没有注意到,因为在过滤器中,die停止执行,但不会中断任何内容。这意味着表单可能不会提交,但前端不会出现任何显示函数已终止的情况。