How to Change the Date and Time Format in Contact Form 7 Mail Tags
Content
Many Contact Form 7 users want to customize the format of the [_date] and [_time] special mail tags for use in email subjects, file names, or message bodies. A common challenge is that the default format includes characters like colons, which are not suitable for filenames, or the full date when only a specific part is needed.
Understanding the Problem
The special mail-tags [_date] and [_time] pull their values from the form submission's timestamp. By default, they use the date and time formats set in your WordPress general settings. While this is convenient, it offers limited flexibility. As seen in the sample threads, users have tried various syntaxes like [_date "m/Y"] or [_time "His"] to override the format, but these attempts are unsuccessful because the plugin's core functionality does not natively support passing format parameters to these specific tags.
Common Solutions and Workarounds
1. Using a Hidden Date Field (The Recommended Workaround)
The most reliable method without custom code is to create a custom date field in your form and use the [_format_...] tag, which does accept a format parameter.
- Add a Date Field to Your Form: In your form template, insert a date field. You can set its default value to "Today" and hide it from view if desired.
[date my-custom-date "Today"]
- Use the _format_tag in Your Mail Template: In your Mail tab, use the following syntax to output the date in your desired format (e.g., month and year).
[_format_my-custom-date "m/Y"]
This workaround is effective because you are controlling the date value and its formatting directly through a form field you define.
2. For the [_time] Tag and File Names
A user wanted to use [_time] in a PDF filename but encountered issues with the colon. It's important to note that special mail-tags are generally not designed to be used within file paths or names due to potential character conflicts.
An attempted solution was to use a third-party shortcode plugin to generate a globally formatted time. However, this often fails because Contact Form 7 does not process standard WordPress shortcodes within its mail template fields by default.
3. Custom Code for Advanced Control
For developers requiring more advanced control, the ultimate solution is to use a custom filter. This involves adding code to your theme's functions.php file or a custom plugin to modify how the mail-tags are processed.
Warning: Always back up your site before editing theme files and consider adding code via a child theme or a custom plugin to prevent loss during updates.
/**
* Custom filter to change the format of the [_time] mail-tag
* This example changes the format to H-i-s (e.g., 14-30-15)
*/
add_filter( 'wpcf7_special_mail_tags', 'custom_wpcf7_time_format', 10, 4 );
function custom_wpcf7_time_format( $output, $name, $html, $mail_tag ) {
if ( '_time' == $name ) {
// Define your custom time format here
$output = current_time( 'H-i-s' ); // Replaces colons with hyphens
}
return $output;
}
This approach provides the highest level of customization but requires comfort with PHP code.
Conclusion
While Contact Form 7's built-in [_date] and [_time] tags are convenient, their formatting options are limited to your site's general settings. For most users, the best approach is to create a custom date field and use the [_format_...] tag. For developers or specific use cases like filename generation, implementing a custom filter offers a powerful and flexible solution.
Related Support Threads Support
-
[_time] format changehttps://wordpress.org/support/topic/_time-format-change/
-
subscriptions substackhttps://wordpress.org/support/topic/subscriptions-substack/
-
Display url tag in post messagehttps://wordpress.org/support/topic/display-url-tag-in-post-message/
-
Formatting the email notificationhttps://wordpress.org/support/topic/formatting-the-email-notification/
-
Change date format of date-mail-tag [_date]https://wordpress.org/support/topic/change-date-format-of-date-mail-tag-_date/
-
Is there a way to pass the form name though email?https://wordpress.org/support/topic/is-there-a-way-to-pass-the-form-name-though-email/
-
add tab character to Mail template, copy contents to spreadsheethttps://wordpress.org/support/topic/add-tab-character-to-mail-template-copy-contents-to-spreadsheet/
-
URL path with parametershttps://wordpress.org/support/topic/url-path-with-parameters/
-
Send title of the post with CF7 from category pagehttps://wordpress.org/support/topic/send-title-of-the-post-with-cf7-from-category-page-2/
-
Display name in templatehttps://wordpress.org/support/topic/display-name-in-template/
-
clone duplicate form cmd linehttps://wordpress.org/support/topic/clone-duplicate-form-cmd-line/
-
Take the results of a drop-down menu and add it to the subject line of the mailhttps://wordpress.org/support/topic/take-the-results-of-a-drop-down-menu-and-add-it-to-the-subject-line-of-the-mail/
-
Extend the tag-generator.jshttps://wordpress.org/support/topic/extend-the-tag-generator-js/
-
duplicate the placeholder value as an aria-label in form-taghttps://wordpress.org/support/topic/duplicate-the-placeholder-value-as-an-aria-label-in-form-tag/