The code snippet below demonstrates how to set dynamic email subjects for WP Forms in WordPress. This can be useful when you want to personalize the subject line of the email based on the user’s input or other dynamic data.
/**
* Set dynamic email subject for WP Forms
*/
function wpsnippets_set_dynamic_email_subject( $subject, $form_data ) {
// Customize the subject based on your requirements
$dynamic_subject = 'New form submission: ' . $form_data['fields']['your-name'];
return $dynamic_subject;
}
add_filter( 'wpforms_mail_subject', 'wpsnippets_set_dynamic_email_subject', 10, 2 );
In this code snippet, we define a custom function wpsnippets_set_dynamic_email_subject
that takes two parameters: $subject
(the default email subject) and $form_data
(an array containing the submitted form data).
Inside the function, you can customize the subject line based on your requirements. In the example, we concatenate the string “New form submission: ” with the value of the “your-name” field from the submitted form.
Finally, we use the add_filter
function to hook into the wpforms_mail_subject
filter and specify our custom function as the callback. This ensures that our dynamic subject is used when sending the email.
By using this code snippet, you can easily set dynamic email subjects for WP Forms, allowing you to personalize the subject line based on user input or other dynamic data.
Examples
Example #1: Changing the email subject based on form input
This use case demonstrates how to dynamically change the email subject of a WP Forms notification based on user input in the form.
function wpsnippets_change_email_subject( $subject, $form_data ) {
if ( $form_data['id'] == 123 ) {
$subject = 'New subject for Form 123';
}
return $subject;
}
add_filter( 'wpforms_notification_email_subject', 'wpsnippets_change_email_subject', 10, 2 );
In this code example, we use the wpforms_notification_email_subject
filter hook to modify the email subject. We check if the form ID matches the desired form (in this case, form ID 123) and update the subject accordingly. You can customize the form ID and the new subject as needed.
Example #2: Including form field values in the email subject
This use case demonstrates how to include specific form field values in the email subject of a WP Forms notification.
function wpsnippets_include_field_values_in_subject( $subject, $form_data ) {
if ( $form_data['id'] == 123 ) {
$name = $form_data['fields'][0]['value'];
$subject .= ' - ' . $name;
}
return $subject;
}
add_filter( 'wpforms_notification_email_subject', 'wpsnippets_include_field_values_in_subject', 10, 2 );
In this code example, we retrieve the value of a specific form field (in this case, the first field) using its index in the $form_data['fields']
array. We then append this value to the existing subject using the .=
concatenation operator. You can modify the form ID and the field index as per your requirements.
Example #3: Using conditional logic to set email subject
This use case demonstrates how to set different email subjects based on conditional logic using WP Forms.
function wpsnippets_conditional_email_subject( $subject, $form_data ) {
if ( $form_data['id'] == 123 ) {
$field_value = $form_data['fields'][0]['value'];
if ( $field_value == 'Option A' ) {
$subject = 'Subject for Option A';
} elseif ( $field_value == 'Option B' ) {
$subject = 'Subject for Option B';
} else {
$subject = 'Default Subject';
}
}
return $subject;
}
add_filter( 'wpforms_notification_email_subject', 'wpsnippets_conditional_email_subject', 10, 2 );
In this code example, we use conditional statements to set different email subjects based on the value of a specific form field. If the form ID matches (in this case, form ID 123), we retrieve the value of the first field and check it against different options. Depending on the value, we update the subject accordingly. You can modify the form ID, field index, and the conditional logic as per your requirements.