Prefix Contact Form 7 Email Subjects
Adds the form name and a short reference number to the subject of every Contact Form 7 email, so messages are easy to filter and refer to in your inbox.
Opens WPIDE on
Code
$wpide_cf7_subject_format = '[%1$s #%2$s] '; // Prefix format.
add_filter( 'wpcf7_mail_components', function ( $components, $contact_form = null ) use ( $wpide_cf7_subject_format ) {
if ( ! $contact_form || ! method_exists( $contact_form, 'title' ) ) {
return $components;
}
$reference = strtoupper( substr( base_convert( (string) ( time() . wp_rand( 10, 99 ) ), 10, 36 ), -6 ) );
$components['subject'] = sprintf( $wpide_cf7_subject_format, $contact_form->title(), $reference ) . $components['subject'];
return $components;
}, 10, 2 );
Notes
Change the format string at the top; %1$s is the form title and %2$s the reference.
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.