Contact Form 7 (CF7) is one of the most popular contact form plugins for WordPress. It offers a variety of functions, including so-called Mail-specific mail tags like [_post_id], [_post_title] or [_post_author_email].
These special tags can be used to easily retrieve information about the post in which a form is embedded. But what happens if a form is embedded outside of the actual post content (e.g. in a sidebar or a widget)? This is exactly where a problem arises.
![Ohne Anpassungen würden Tags wie [_post_author_email] oder [_post_title] im Contact Form 7 Formular leer bleiben.](/wp-content/uploads/2025/06/msedge_2hXzheRjQb.png)
Why we needed and optimized our sidebar form
When we introduced our sidebar form at Konfuzio, we wanted to offer readers an easier and more targeted way to get in touch with the authors of our articles. It was particularly important to us that we made the process efficient and secure for both readers and authors. From there, an exciting task arose: how can we dynamically integrate the author's email address into the form without revealing it publicly in the frontend?
Why the form is so important to us:
- Direct contact with the author/contact interface for readers
Readers often have specific questions or want to delve deeper into the topic covered in the article. Our aim was to build a direct bridge to the authors of the content - not via an anonymous feedback system, but via a targeted and context-driven communication tool. At the same time, we were able to prevent the author's email address from being directly visible in the frontend in order to avoid spam or misuse. - Targeted feedback for continuous quality improvement
Good feedback is essential in order to create and further develop high-quality content. With this form, we give readers the opportunity not only to ask direct questions, but also to provide feedback on our content. What topics concern them? Where might there still be gaps in the information? This feedback is forwarded directly to our editorial and author teams and helps to improve content in the long term. - Triggers for regular editing and updates of posts
Another positive effect of the form is that it reminds our authors to check their posts regularly and add up-to-date information on an ongoing basis. A reader requesting or commenting on a post is a sign that the content remains relevant to our target audience. This was motivation enough for us to work on a robust solution that would also enable this function in the sidebar without restrictions. - Praise and reward for committed authors and good content
Content that activates readers and encourages a direct exchange with the authors is a sign for us that we are not only developing topics, but also building relationships with our community. Giving the author of a post the opportunity to have contact with motivated readers is a kind of reward for the effort that has gone into the content - an incentive to create more high-quality content.
The technical challenge
One of our biggest obstacles was that Contact Form 7 does not include a [_post_*]-tags are supported when forms are integrated outside the content of a post - as in our case in the sidebar. Without adjustments, tags such as [_post_author_email] or [_post_title] remain empty.
We worked around this problem with a customized solution. Thanks to the dynamic integration of the post ID as a hidden field, we were able to fill the tags on the server side and securely retrieve all context information. With this solution, our form now also works reliably in the sidebar, and our readers and authors benefit equally from a smooth feedback process.
How the mail-specific mail tags work
The above [_post_*]-tags in CF7 make it possible to automatically integrate data from the post into the e-mail that is sent when the form is submitted.
These include:
[_post_id]: Returns the ID of the current post.[_post_name]: The slug (name) of the post.[_post_title]: The title of the respective post.[_post_url]: The URL of the post.[_post_author]: The name of the author of the post.[_post_author_email]: The e-mail address of the post author.
In theory, these tags are a clever tool for integrating dynamic content depending on the post, without having to manually create a separate form for each post.
Problem:
These [_post_*]-tags work, however onlyif the form is part of the Post content is embedded within the content area. However, if the form is embedded in the Sidebar, in a widget or a theme file (such as single.php) is loaded, these special tags Empty. This is because CF7 does not automatically recognize the post context outside of the actual content.
Why do these problems occur?
Contact Form 7 relies on the form being rendered in an environment in which the WordPress main query sets the current post correctly. However, if a form is integrated into a sidebar or a widget, there is no direct reference to the main query. In this case, CF7 does not know which post is currently being displayed and fills in the [_post_*]-tags are therefore not sufficient.
Example: A form in the sidebar
Assuming you have created a blog post with the ID 123 and a CF7 form in the sidebar. The form should actually have the title My Blog Post and the e-mail address of the post author. However, as the form is rendered outside the post, the tags remain as [_post_title] and [_post_author_email] blank.
Solution: Dynamic detection of the post context
To solve this problem, we can use the Add post context manually and on the server sideso that the required tags work in any environment - even in the sidebar or in widgets. Here we show a solution with two simple steps:
Step 1: Add post ID as hidden field
First of all, we make sure that the current Postal ID is added to every form, regardless of where it is embedded. For this we use the hook wpcf7_form_hidden_fieldsto display a hidden field with the Post-ID (post_id) into the form:
/**
* Add the current post ID as a hidden field in CF7 forms.
*/
add_filter('wpcf7_form_hidden_fields', function ($hidden_fields) {
if (is_singular()) {
global $post;
$post_id = get_queried_object_id() ?: ($post->ID ?? null);
if ($post_id) {
$hidden_fields['post_id'] = absint($post_id); // Ensure the post ID is valid and sanitized.
}
}
return $hidden_fields; // Return the modified hidden fields.
});Explanation:
is_singular()Checks whether the current post is a single page, a single post or a user-defined post type.- The Post ID is used as
post_idin the field$_POSTof the form and is therefore available when the form is processed.
Step 2: Dynamic processing of the _post_*-Tags
Now we will implement a hook that will [_post_*]-tags dynamically during form processing. This approach checks whether a post ID exists and the corresponding data is retrieved dynamically.
/**
* Dynamically populate all _post_* tags using the post ID passed as a hidden field.
*/
add_filter('wpcf7_special_mail_tags', function ($output, $tag_name) {
// Process only _post_* tags.
if (preg_match('/^_post_(id|name|title|url|author|author_email)$/', $tag_name, $matches)) {
// Retrieve the post_id from the hidden fields in the form submission (sent through POST).
$post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : null;
// If a valid post ID is available, resolve the tag dynamically.
if ($post_id) {
switch ($tag_name) {
case '_post_id':
return $post_id;
case '_post_name':
return get_post_field('post_name', $post_id); // Retrieve the post slug.
case '_post_title':
return get_the_title($post_id);
case '_post_url':
return get_permalink($post_id);
case '_post_author':
$author_id = get_post_field('post_author', $post_id);
return get_the_author_meta('display_name', $author_id);
case '_post_author_email':
$author_id = get_post_field('post_author', $post_id);
$author_email = get_the_author_meta('user_email', $author_id);
return !empty($author_email) ? esc_html($author_email) : 'fallback@example.com';
default:
return $output;
}
}
// Return an empty value if the post ID is not available.
return '';
}
// Default output for tags not matching _post_*.
return $output;
}, 10, 2);Explanation:
- Through the integration in
wpcf7_special_mail_tagswe can treat each day individually. - The data (
_post_id,_post_author_emailetc.) are server-side retrieved on the basis of the Post-ID.
Application example
- In the sidebar:
Bind the form within the sidebar:Error: Contact form not found.
<aside>
<?php echo do_shortcode('[contact-form-7 id="123"]'); ?>
</aside>- CF7 mail settings:
Use the_post_*-tags in the mail configuration:
Post ID: [_post_id]
Post Name: [_post_name]
Post Title: [_post_title]
Post URL: [_post_url]
Post Author: [_post_author]
Post Author Email: [_post_author_email]Result:
Although the form is embedded in the sidebar, the values are added correctly.
Conclusion
The use of post-specific mail tags in Contact Form 7 outside of the post content is problematic by default. By making the adjustments described above, we can ensure that all relevant mail data is also reliably available in the sidebar or widgets. This solution is data protection-friendly, secure and dynamic - and allows the _post_*-tags work again as usual.
