WordPress contact form 7 text value change

2019-03-30 16:54发布

WordPress - Contact Form 7

I am trying to find out the filter to modify the cf7 field value when someone enter values in it.

when user type in textfield and submit data,

  1. validate - I had done
  2. should not goto thank you page if invalid entry - I had done
  3. replace text field with new data - Not Done

Eg: 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;
}

Eg: 2

another working example

everything working except $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条回答
Summer. ? 凉城
2楼-- · 2019-03-30 17:16

What you are trying to do cannot be done with only the validation filter. Because that just outputs true or false based on the validations performed. To do what you want, you have to use another filter ( 'wpcf7_posted_data' ) that has the value you want to filter. So we can break down our process into two steps.

Step 1: Do all the validation like you are currently doing.

Using your Example 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 );

The above code will make sure that your points 1 and 2 are working.

  1. Validate.
  2. Stop submission if entry is invalid.

Step 2: Re-run your tests to get the desired value and update it.

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. This will update the values sent in the email, or in the submissions if you store them. It will show the validation message, but it will not show the updated value in text field because that cannot be done with php in this scenario.

P.S. 2 This is all tested code. Happy Coding.

查看更多
地球回转人心会变
3楼-- · 2019-03-30 17:31

maybe this can help:

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

Hope it helps!

P.S. This is not tested, please let me know if it works :)

查看更多
登录 后发表回答