How to Include Field Descriptions Inside Email Notifications

Overview

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.

Setup

In order to include the descriptions inside the email notifications, you’ll need to copy this code snippet to your site.

/*
 * Include field descriptions inside email notifications.
 *
 * @link https://wpforms.com/developers/how-to-include-field-descriptions-inside-email-notifications/
 *
*/

// HTML Email.
add_filter( 'wpforms_html_field_value', static function ( $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:5px;background-color:yellow;"><i>' . $field_data['description'] . '</i></p>';
}, 10, 4 );

// Plain text email.
add_filter( 'wpforms_plaintext_field_value', static function ( $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";
}, 10, 3 );

Please know that inside the HTML code snippet is some CSS inline styling to style these descriptions differently. You’ll need to remember to change this line of code <p style="padding:5px;background-color:yellow;"><i>' . $field_data['description'] . '</i></p> to match the styling you want to apply.

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.