• I am looking to reformat a text field before the form is saved. The issue is people tend to get lazy when inputting a phone number or postal code. ex: when a postal code is entered this way ‘h0h0h0’ or ‘h0h 0h0’ when saved I want it to be saved as ‘H0H 0H0’.

    I found a way to reformat the field data in the email body before it is sent, however, I have the form saving into the database where the form data is saved before it is made into an email message.

    I am attempting to set up a filter using wpcf7_posted_data_text

    function alter_wpcf7_posted_data( $data ) {
    if (isset( $data['postal-code'] )) {
    $pc_data = $data['postal-code'];
    $pc_format = '/^[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d$/';
    if ( !empty( $pc_data ) && preg_match( $pc_format, $pc_data , $matches ) ) {
    $pc_data = preg_replace( '/([A-Za-z]\d[A-Za-z]) ?(\d[A-Za-z]\d)/', '$1 $2', $pc_data );
    $pc_data = strtoupper( $pc_data );
    $data['postal-code'] = $pc_data;
    }
    }
    return $data;

    }

    add_filter('wpcf7_posted_data_text*', 'alter_wpcf7_posted_data', 10, 1 );
    add_filter('wpcf7_posted_data_text', 'alter_wpcf7_posted_data', 10, 1 );

    When I run this, the form submission stalls out. I am having difficulty finding out how to handle the $data from the wpcf7_posted_data_text hook.

    • This topic was modified 2 months, 2 weeks ago by schulz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    The filter hook is used here:

    https://github.com/rocklobster-in/contact-form-7/blob/v5.9.8/includes/submission.php#L389

    As you see in the code, the callback function takes three arguments (not one), and the first one ($data in your code) isn’t an array.

    Thread Starter schulz

    (@schulz)

    As you probably already can tell, I am not a programmer. I try to piece together code from code I see from examples:

    add_filter( 'wpcf7_posted_data' , 'alter_wpcf7_posted_data' );

    /**
    * Function for
    wpcf7_posted_data filter-hook.
    *
    * @param $posted_data
    *
    * @return
    */
    function alter_wpcf7_posted_data( $posted_data ) {

    // filter...
    return $posted_data ;
    }

    In your link; lines 389 – 393 show three variables ($value, $value_orig, and $tag) but do not show much else. I found a better example that resembles more of what you are saying:

    // define the wpcf7_posted_data_{type} callback 
    function custom_wpcf7_posted_data_{type}( $value, $value_orig, $tag ){
    //custom code here
    return $value
    }

    //add the action
    add_filter('wpcf7_posted_data_{type}', 'custom_wpcf7_posted_data_{type}', 10, 3)

    The ‘//custom code here’ is what I am after. How do you extract the correct field data, modify it, and then write it back to the proper field data?

    I assume the ‘$tag’ variable has the field name and is likely an array [$tag->name] and that is where it goes beyond me on how to do this.

    • This reply was modified 2 months, 1 week ago by schulz.
    Thread Starter schulz

    (@schulz)

    I am looking to reformat a text field before the form is saved, has anyone done this with PHP or do I have to do something with JavaScript?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.