Introduction
Would you like to include Field Descriptions inside your email notifications? By default, the {all_fields} includes all form fields and labels that are filled in from the form with the exception of descriptions. However, you can easily include descriptions inside email notifications using a small PHP snippet. In this tutorial, we’ll walk you through each step on how to achieve this!
Creating your form
First, you’ll need to create your form. Our form will have just a few fields included and only one field will have the Field Description entered.
Once you’ve created your form, just complete the Field Descriptions you want to include in your email notification.
If you need any assistance in creating your form, please visit this documentation.
Using the HTML or Plain Text Email Template
In this tutorial, we’re going to give you both code snippets that will include your Field Descriptions inside your email notifications regardless if you are using the HTML Email Template or the Plain text emails.
If you’re not sure which setting you have or what this setting is, you can always review this documentation to see where to change this option.
For our tutorial, we’re using the HTML Email Template.
Adding the snippet
Now it’s time to add the snippet to your site. If you need any help in adding snippets to your site, please see this tutorial.
/* * Include field descriptions inside email notifications. * * @link https://wpforms.com/developers/how-to-include-field-descriptions-inside-email-notifications/ */ // HTML Email Template snippet function wpf_dev_html_field_value($field_val, $field, $form_data, $context) { if ( $context !== 'email-html' ) { return $field_val; } if ( empty( $form_data[ 'fields' ][ $field[ 'id' ] ] ) ) { return $field_val; } $field_data = $form_data[ 'fields' ][ $field[ 'id' ] ]; if ( empty( $field_data[ 'description' ] ) ) { return $field_val; } return $field_val . '<p>' . $field_data[ 'description' ] . '</p>'; } add_filter( 'wpforms_html_field_value', 'wpf_dev_html_field_value', 10, 4 ); // Plain text email snippet function wpf_dev_plaintext_field_value($field_val, $field, $form_data) { if ( empty( $form_data[ 'fields' ][ $field[ 'id' ] ] ) ) { return $field_val; } $field_data = $form_data[ 'fields' ][ $field[ 'id' ] ]; if ( empty( $field_data[ 'description' ] ) ) { return $field_val; } $field_val = str_replace( [ "\r", "\n" ], '', $field_val ); return $field_val . "\r\n" . $field_data[ 'description' ] . "\r\n\r\n"; } add_filter( 'wpforms_plaintext_field_value', 'wpf_dev_plaintext_field_value', 10, 3 );
By adding this snippet, your field descriptions will be included in all of your email notifications.
And that’s it! You’ve now included the Field Descriptions inside your email notifications! Would you like to also hide image choices from email notifications? Take a look at the article on How to Hide Image Choices in Notification Emails.
FAQ
Q: Can I style the Field Descriptions in the notification?
A: If you’re using the HTML Email Template for your email notifications, you can add some CSS styling to the Field Descriptions by using this snippet.
/* * Include field descriptions inside email notifications. * * @link https://wpforms.com/developers/how-to-include-field-descriptions-inside-email-notifications/ */ function wpf_dev_html_field_value($field_val, $field, $form_data, $context) { if ( $context !== 'email-html' ) { return $field_val; } if ( empty( $form_data[ 'fields' ][ $field[ 'id' ] ] ) ) { return $field_val; } $field_data = $form_data[ 'fields' ][ $field[ 'id' ] ]; if ( empty( $field_data[ 'description' ] ) ) { return $field_val; } return $field_val . '<p style="padding:10px;background-color:#f6f6f6; color: #333333;"><i>' . $field_data[ 'description' ] . '</i></p>'; } add_filter( 'wpforms_html_field_value', 'wpf_dev_html_field_value', 10, 4 );
Q: Can I target just 1 form by the form ID?
A: Absolutely! If you’d only like to do this for one form, use this snippet.
/* * Include field descriptions inside email notifications for a specific form. * * @link https://wpforms.com/developers/how-to-include-field-descriptions-inside-email-notifications/ */ // HTML Email Template snippet function wpf_dev_html_field_value($field_val, $field, $form_data, $context) { // Only process this on form ID 1184 if ( $form_data['id'] !== '1184' ) { return $field_val; } if ( $context !== 'email-html' ) { return $field_val; } if ( empty( $form_data[ 'fields' ][ $field[ 'id' ] ] ) ) { return $field_val; } $field_data = $form_data[ 'fields' ][ $field[ 'id' ] ]; if ( empty( $field_data[ 'description' ] ) ) { return $field_val; } return $field_val . '<p>' . $field_data[ 'description' ] . '</p>'; } add_filter( 'wpforms_html_field_value', 'wpf_dev_html_field_value', 10, 4 );
You’ll need to replace the 1184 in the snippet to match your own form ID. If you need help in finding your form ID, please review this tutorial.
Q: How can I add the field descriptions to the entries?
A: You can include field descriptions when viewing your form entries by adding this snippet to your site.
/* * Include field descriptions inside form entries. * * @link https://wpforms.com/developers/how-to-include-field-descriptions-inside-email-notifications/ */ function wpf_dev_html_field_value($field_val, $field, $form_data, $context) { if ( $context !== 'entry-single' ) { return $field_val; } if ( empty( $form_data['fields'][ $field['id'] ] ) ) { return $field_val; } $field_data = $form_data['fields'][ $field['id'] ]; if ( empty( $field_data['description'] ) ) { return $field_val; } return $field_val . '<br><br>' . $field_data['description']; } add_filter( 'wpforms_html_field_value', 'wpf_dev_html_field_value', 10, 4 );
By using the entry-single
we’re allowing the field descriptions to be viewed when viewing a single entry. Field descriptions will not be permanently saved on all entries using this snippet on the current entry being viewed will have the field descriptions displaying on screen.