Last updated on October 18, 2023

WP Forms email notifications

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Configure email notifications in WP Forms.

To set up email notifications for WP Forms in WordPress, you can use the wp_mail() function to send an email whenever a form is submitted. You can customize the email content and recipients based on your specific requirements.

Here’s an example code snippet that demonstrates how to send an email notification when a WP Forms form is submitted:

/**
 * Send email notification when WP Forms form is submitted
 */
function wpsnippets_wpforms_email_notification( $fields, $entry, $form_data ) {
    // Get form ID
    $form_id = $form_data['id'];

    // Customize email subject and content
    $subject = 'New form submission from WP Forms';
    $message = 'A new form has been submitted with the following details:' . "nn";

    // Loop through form fields and add them to the email message
    foreach ( $fields as $field ) {
        $field_label = $field['label'];
        $field_value = $entry[ $field['id'] ];

        $message .= $field_label . ': ' . $field_value . "n";
    }

    // Customize email recipients
    $to = 'admin@example.com';

    // Send the email
    wp_mail( $to, $subject, $message );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_email_notification', 10, 3 );

In this code snippet, we define a custom function wpsnippets_wpforms_email_notification that is hooked to the wpforms_process_complete action. This action is triggered when a WP Forms form is submitted.

Inside the function, we first retrieve the form ID using the $form_data parameter. This allows you to differentiate between different forms if you have multiple forms on your site.

Next, we customize the email subject and content according to your needs. In the example, we set a generic subject and include the form submission details in the email message.

Then, we loop through the form fields using the $fields parameter and add their labels and values to the email message.

After that, you can customize the email recipients by modifying the $to variable. In the example, we set it to a static email address, but you can dynamically set it based on your requirements.

Finally, we use the wp_mail() function to send the email with the specified subject, message, and recipients.

This code snippet can be useful in scenarios where you want to receive email notifications whenever a WP Forms form is submitted. It allows you to stay updated with form submissions and take necessary actions promptly.

Examples

Example 1: Customizing WP Forms email notifications

This use case demonstrates how to customize the email notifications sent by WP Forms plugin.

function wpsnippets_custom_wpforms_email_notification( $email, $form_data ) {
    // Modify the email subject
    $email['subject'] = 'New form submission from ' . $form_data['fields'][0]['value'];

    // Modify the email body
    $email['message'] .= "nnCustom message added to the email body.";

    return $email;
}
add_filter( 'wpforms_email_notification', 'wpsnippets_custom_wpforms_email_notification', 10, 2 );

In this code example, we define a custom function wpsnippets_custom_wpforms_email_notification that takes two parameters: $email (an array containing the email data) and $form_data (an array containing the submitted form data). We then modify the email subject by appending the value of the first form field to it, and add a custom message to the email body. Finally, we return the modified $email array. The function is hooked into the wpforms_email_notification filter using add_filter().

Example 2: Sending additional email notifications

This use case demonstrates how to send additional email notifications when a WP Forms submission is received.

function wpsnippets_send_additional_email_notification( $fields, $entry, $form_data ) {
    $additional_email = 'additional@example.com';
    $subject = 'New form submission from ' . $fields[0]['value'];
    $message = 'A new form submission has been received.';

    wp_mail( $additional_email, $subject, $message );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_additional_email_notification', 10, 3 );

In this code example, we define a custom function wpsnippets_send_additional_email_notification that takes three parameters: $fields (an array containing the form field values), $entry (an array containing the form entry data), and $form_data (an array containing the form data). We then define the additional email address, subject, and message. Finally, we use the wp_mail() function to send the additional email. The function is hooked into the wpforms_process_complete action using add_action().

Example 3: Conditionally sending email notifications

This use case demonstrates how to conditionally send email notifications based on the form field values.

function wpsnippets_conditional_email_notification( $email, $form_data ) {
    $send_email = false;

    // Check if a specific form field value meets the condition
    if ( $form_data['fields'][0]['value'] === 'send_email' ) {
        $send_email = true;
    }

    // Send email only if the condition is met
    if ( $send_email ) {
        wp_mail( $email['to'], $email['subject'], $email['message'] );
    }

    return $email;
}
add_filter( 'wpforms_email_notification', 'wpsnippets_conditional_email_notification', 10, 2 );

In this code example, we define a custom function wpsnippets_conditional_email_notification that takes two parameters: $email (an array containing the email data) and $form_data (an array containing the submitted form data). We initialize a variable $send_email to false and then check if a specific form field value meets the condition. If the condition is met, we set $send_email to true and use the wp_mail() function to send the email. Finally, we return the $email array. The function is hooked into the wpforms_email_notification filter using add_filter().

Last updated on October 18, 2023. Originally posted on November 4, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *