Working with wpcf7_posted_data_text
-
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.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.