WordPress contact form 7 text value change












4















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

?>









share|improve this question

























  • Your question is very broad. I suggest looking at the official documentation and the hook documentation

    – janw
    Nov 22 '18 at 11:08











  • @janw what I want is very simple, the instance name to change the form value

    – J.K
    Nov 22 '18 at 11:23











  • @J.K can you please share your contact form code?

    – Mukesh Panchal
    Nov 28 '18 at 4:53











  • @MukeshPanchal please see Eg: 2

    – J.K
    Nov 28 '18 at 5:05











  • Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

    – Nadav
    Nov 28 '18 at 5:18
















4















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

?>









share|improve this question

























  • Your question is very broad. I suggest looking at the official documentation and the hook documentation

    – janw
    Nov 22 '18 at 11:08











  • @janw what I want is very simple, the instance name to change the form value

    – J.K
    Nov 22 '18 at 11:23











  • @J.K can you please share your contact form code?

    – Mukesh Panchal
    Nov 28 '18 at 4:53











  • @MukeshPanchal please see Eg: 2

    – J.K
    Nov 28 '18 at 5:05











  • Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

    – Nadav
    Nov 28 '18 at 5:18














4












4








4


3






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

?>









share|improve this question
















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

?>






php wordpress contact-form-7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 10:16









James Jones

2,79951838




2,79951838










asked Nov 22 '18 at 10:52









J.KJ.K

1,2191725




1,2191725













  • Your question is very broad. I suggest looking at the official documentation and the hook documentation

    – janw
    Nov 22 '18 at 11:08











  • @janw what I want is very simple, the instance name to change the form value

    – J.K
    Nov 22 '18 at 11:23











  • @J.K can you please share your contact form code?

    – Mukesh Panchal
    Nov 28 '18 at 4:53











  • @MukeshPanchal please see Eg: 2

    – J.K
    Nov 28 '18 at 5:05











  • Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

    – Nadav
    Nov 28 '18 at 5:18



















  • Your question is very broad. I suggest looking at the official documentation and the hook documentation

    – janw
    Nov 22 '18 at 11:08











  • @janw what I want is very simple, the instance name to change the form value

    – J.K
    Nov 22 '18 at 11:23











  • @J.K can you please share your contact form code?

    – Mukesh Panchal
    Nov 28 '18 at 4:53











  • @MukeshPanchal please see Eg: 2

    – J.K
    Nov 28 '18 at 5:05











  • Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

    – Nadav
    Nov 28 '18 at 5:18

















Your question is very broad. I suggest looking at the official documentation and the hook documentation

– janw
Nov 22 '18 at 11:08





Your question is very broad. I suggest looking at the official documentation and the hook documentation

– janw
Nov 22 '18 at 11:08













@janw what I want is very simple, the instance name to change the form value

– J.K
Nov 22 '18 at 11:23





@janw what I want is very simple, the instance name to change the form value

– J.K
Nov 22 '18 at 11:23













@J.K can you please share your contact form code?

– Mukesh Panchal
Nov 28 '18 at 4:53





@J.K can you please share your contact form code?

– Mukesh Panchal
Nov 28 '18 at 4:53













@MukeshPanchal please see Eg: 2

– J.K
Nov 28 '18 at 5:05





@MukeshPanchal please see Eg: 2

– J.K
Nov 28 '18 at 5:05













Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

– Nadav
Nov 28 '18 at 5:18





Contact Form 7's action hooks don't allow for changing input fields. You'll have to add a DOM event that triggers on error and then check and update the fields in the DOM as necessary via JavaScript instead. See contactform7.com/dom-events

– Nadav
Nov 28 '18 at 5:18












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer

































    0














    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.






    share|improve this answer


























    • This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

      – Rick Hellewell
      Feb 1 at 21:13











    • Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

      – shazyriver
      Feb 2 at 6:32











    • Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

      – Rick Hellewell
      Feb 2 at 17:55











    • Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

      – shazyriver
      Feb 2 at 18:34











    • However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

      – shazyriver
      Feb 2 at 18:36











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429325%2fwordpress-contact-form-7-text-value-change%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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






    share|improve this answer






























      0














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






      share|improve this answer




























        0












        0








        0







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






        share|improve this answer















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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 23:36

























        answered Nov 28 '18 at 23:27









        Stefan VasiljevicStefan Vasiljevic

        12




        12

























            0














            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.






            share|improve this answer


























            • This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

              – Rick Hellewell
              Feb 1 at 21:13











            • Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

              – shazyriver
              Feb 2 at 6:32











            • Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

              – Rick Hellewell
              Feb 2 at 17:55











            • Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

              – shazyriver
              Feb 2 at 18:34











            • However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

              – shazyriver
              Feb 2 at 18:36
















            0














            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.






            share|improve this answer


























            • This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

              – Rick Hellewell
              Feb 1 at 21:13











            • Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

              – shazyriver
              Feb 2 at 6:32











            • Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

              – Rick Hellewell
              Feb 2 at 17:55











            • Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

              – shazyriver
              Feb 2 at 18:34











            • However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

              – shazyriver
              Feb 2 at 18:36














            0












            0








            0







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 4 '18 at 9:12

























            answered Dec 4 '18 at 9:05









            shazyrivershazyriver

            1,38821637




            1,38821637













            • This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

              – Rick Hellewell
              Feb 1 at 21:13











            • Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

              – shazyriver
              Feb 2 at 6:32











            • Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

              – Rick Hellewell
              Feb 2 at 17:55











            • Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

              – shazyriver
              Feb 2 at 18:34











            • However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

              – shazyriver
              Feb 2 at 18:36



















            • This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

              – Rick Hellewell
              Feb 1 at 21:13











            • Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

              – shazyriver
              Feb 2 at 6:32











            • Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

              – Rick Hellewell
              Feb 2 at 17:55











            • Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

              – shazyriver
              Feb 2 at 18:34











            • However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

              – shazyriver
              Feb 2 at 18:36

















            This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

            – Rick Hellewell
            Feb 1 at 21:13





            This doesn't appear to work in CF7 v 5.0.5. If I put a 'die();' right after the function name, and submit the form, the die() never happens. Any ideas?

            – Rick Hellewell
            Feb 1 at 21:13













            Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

            – shazyriver
            Feb 2 at 6:32





            Can you please update the plugin ? current version is 7.5.1.1 although i don't think that these filters were added recently. I can't get the version 5.0.5 to test it on that.

            – shazyriver
            Feb 2 at 6:32













            Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

            – Rick Hellewell
            Feb 2 at 17:55





            Contact Form 7 is at version 5.1.1, according to the WP repository page. I have not upgraded to 5.1x because of issues with how it forces use of reCAPTCHA v3, and that effect on load time and reCAPTCHA load on entire site (also, see their support forum for massive number of complaints about increased spam).

            – Rick Hellewell
            Feb 2 at 17:55













            Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

            – shazyriver
            Feb 2 at 18:34





            Ah. My Bad. You are right about the version. I misinterpreted it. But i remember testing it at the time of posting this solution. But i don't have time and resources to test this ATM.

            – shazyriver
            Feb 2 at 18:34













            However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

            – shazyriver
            Feb 2 at 18:36





            However, i sure can guide you about the die() you are doing. It happens, but you do not notice it because, in filters die stops execution but it does not disrupt anything. Which means probably form would not submit but nothing on the front end will happen that shows the function died.

            – shazyriver
            Feb 2 at 18:36


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429325%2fwordpress-contact-form-7-text-value-change%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()