Why _post_title Only Returns the First Entry and How to Fix It
When integrating Contact Form 7 into a dynamic catalog where each submission represents a specific product, the default behavior of the plugin can be misleading. The form tag [_post_title] is designed to capture the title of the current page being viewed. In your scenario, where you are rendering individual product pages (like the LED light components example), this tag correctly outputs that specific product's name.
However, relying on this tag inside a single form submission creates a fundamental architectural limitation: a standard Contact Form 7 instance processes only one request at a time. When a user submits the "Request Quote" form on the product page for led-light-components, the server receives a single POST request containing the data from that specific session. The global variable or tag context associated with _post_title resolves to the current URL (led-light-components) during that specific request lifecycle. It does not have access to a history of previous requests or a list of all products on the site.
The core issue stems from misunderstanding how Contact Form 7 handles multi-page data collection versus single-form submissions. If you attempt to submit one form for multiple products, the backend email recipient will only see the data corresponding to the most recent request that populated the form fields. Since the form fields are static (e.g., Name, Message) and do not dynamically change based on the product list in a way that Contact Form 7 tracks as separate "submissions" per item, you effectively get one email per click, not one email per product list.
To achieve the goal of sending an email containing a list of all product titles associated with the quote request, you need to decouple the data collection from the standard single-form logic. There are three primary approaches to solving this, ranging from simple field manipulation to server-side looping.
Approach 1: Custom Fields for Bulk Submission
The most robust solution without modifying core plugin files is to change how the user submits the data. Instead of expecting the form to automatically detect a list of products, you must explicitly ask the user or system to provide them in a format that can be parsed.
You can add a custom text area field to your Contact Form 7 setup:
[textarea quote_items]
In your frontend PHP template (where the product loop usually runs), when iterating through your products, you append the title and ID to this specific textarea value before saving it or sending the email. This requires a slight modification to how the form is rendered or a custom snippet that injects the current page's title into the quote_items field automatically if you are using a shortcode loop.
Alternatively, if your workflow involves generating a CSV or JSON payload on the client side that gets pasted into this field, the email template can then use [_quote_items] instead of [_post_title]. This ensures the email content reflects the aggregate list rather than just the current context.
Approach 2: Server-Side Processing with wpcf7_mail_sent
If you cannot alter the form fields easily, you must handle the aggregation on the server side after the email is prepared but before it is sent, or intercept the submission to create a batch process. Contact Form 7 provides the wpcf7_mail_sent action hook, which fires every time an email is successfully sent.
You can hook into this event to check if the current submission originated from a multi-product context. However, since standard Contact Form 7 submissions are stateless per request, you would need to store the list of product titles in a temporary database record or a session variable keyed by the user's IP or a unique token they generated during their browsing session.
Here is a conceptual example using wpcf7_mail_sent to override the email body if a batch ID exists:
add_action( 'wpcf7_mail_sent', function( $contact_form_submission ) {
// Check if a specific batch identifier exists in the database or session
// This assumes you stored the list of product titles when they viewed items
$batch_id = get_transient( 'cf7_quote_batch_' . $contact_form_submission->entry_id );
if ( $batch_id ) {
$titles = get_transient_data( 'cf7_quote_batch_' . $batch_id );
// Construct a new message body with the list of titles
$new_message = "Requested Quote for the following products:\n\n";
foreach ( $titles as $title ) {
$new_message .= "- {$title}\n";
}
// You cannot directly modify $contact_form_submission->message_body
// via this hook alone without customizing the mailer class or using a filter.
// A cleaner approach is to use wpcf7_mail_before.
}
} );
A better implementation strategy utilizes the wpcf7_mail_before action, which allows you to modify the mail object properties before the SMTP or mail function executes. You can inject a custom header or body content that concatenates all stored product titles.
Approach 3: Custom Mailer Class
If the dynamic nature of your site requires complex logic (like fetching titles from a database based on a list of IDs submitted in the message), creating a custom mailer class is often cleaner than hacking the default template. You can define a class that extends WPCF7_ContactForm or hooks into the email sending process to replace the default mail function with your own logic.
This approach gives you full control over how the recipient address, subject line, and body content are constructed. You can write a custom PHP file in your theme's folder (e.g., functions.php) that defines a new mailer instance. This ensures that when the email is dispatched, it pulls the necessary data from your database or temporary storage rather than relying on the static [_post_title] tag which is bound to a single URL context.
Best Practices and Considerations
When implementing these solutions, keep in memory that Contact Form 7 is not designed for complex multi-step data aggregation out of the box. The _post_title tag is a convenience feature for single-page forms. If you require batch processing, treating the form submission as a transaction event rather than a static template render is the correct mental model.
Using custom fields to explicitly pass the list of items is generally more reliable than trying to reverse-engineer the current page state after the fact. It shifts the responsibility of data preparation to the frontend logic (or your backend loop) before it hits the form submission endpoint, ensuring the email template receives exactly what you intended: a complete list of product titles ready for the vendor to review.