# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, notifications, subject
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-subject-prefix/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Prefix Contact Form 7 Email Subjects" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
