Tidy Up Contact Form 7 Submissions
Trims stray spaces, lowercases email addresses and collapses repeated blank lines in Contact Form 7 submissions before they are emailed or stored.
Opens WPIDE on
Code
add_filter( 'wpcf7_posted_data', function ( $data ) {
foreach ( (array) $data as $key => $value ) {
if ( is_array( $value ) || 0 === strpos( $key, '_' ) ) {
continue;
}
$value = trim( preg_replace( "/(\r?\n){3,}/", "\n\n", (string) $value ) );
if ( false !== stripos( $key, 'email' ) ) {
$value = strtolower( $value );
}
$data[ $key ] = $value;
}
return $data;
} );
Notes
Field names containing "email" are lowercased; everything else keeps its case.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.