WordPress联系人表格7文本值更改

时间:2018-11-22 10:52:37

标签: php wordpress contact-form-7

WordPress-联系表7

当有人在其中输入值时,我正在尝试找出用于修改cf7字段值的过滤器。

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

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

例如: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_cleaned_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 );

    ?>

2 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

Class clazz = Class.forName("javax.el.MethodExpression");
Object obj = clazz.newInstance();
MethodExpression me = (MethodExpression) obj;
Field field = me.getField("serialVersionUID");

希望有帮助!

P.S。这未经测试,请让我知道是否有效:)

答案 1 :(得分:0)

仅使用验证过滤器无法完成您尝试做的事情。因为这只是根据执行的验证输出true或false。要执行您想要的操作,您必须使用另一个具有您要过滤的值的过滤器(' wpcf7_posted_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点正常工作。

  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 );

PS 。这将更新电子邮件中或存储的提交中发送的值。它会显示验证消息,但不会在文本字段中显示更新的值,因为在这种情况下用php无法完成。

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