Home / Archive / WPForms: add field description in notification HTML and plain text emails
Duplicate Snippet

Embed Snippet on Your Site

WPForms: add field description in notification HTML and plain text emails

With this snippet, you can add a field description in notification emails that are sent to admins/users. They are visually separated.

Code Preview
php
<?php
// 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 );

Comments

Add a Comment