Last updated on October 18, 2023

WP Forms conditional notifications

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

Send targeted notifications in WP Forms.

Conditional notifications in WP Forms allow you to send different email notifications based on the user’s input in the form. This can be useful in scenarios where you want to send specific notifications to different recipients or departments based on the form submission.

To achieve conditional notifications in WP Forms, you can use the wpforms_email_recipient filter hook. This hook allows you to modify the email recipient(s) dynamically before the notification is sent.

Here’s an example code snippet that demonstrates how to use conditional notifications in WP Forms:

/**
 * Modify email recipient based on form input.
 *
 * @param string $recipient Email recipient.
 * @param array  $fields    Form fields.
 * @param array  $form_data Form data.
 *
 * @return string Modified email recipient.
 */
function wpsnippets_modify_email_recipient( $recipient, $fields, $form_data ) {
    // Check if the form ID matches the desired form.
    if ( $form_data['id'] === 123 ) {
        // Get the value of a specific form field.
        $field_value = wpforms()->process->fields( $fields, 'field_id' );

        // Modify the recipient based on the field value.
        if ( $field_value === 'option1' ) {
            $recipient = 'recipient1@example.com';
        } elseif ( $field_value === 'option2' ) {
            $recipient = 'recipient2@example.com';
        } else {
            $recipient = 'default@example.com';
        }
    }

    return $recipient;
}
add_filter( 'wpforms_email_recipient', 'wpsnippets_modify_email_recipient', 10, 3 );

In the above code, we define a custom function wpsnippets_modify_email_recipient that takes three parameters: $recipient, $fields, and $form_data. The $recipient parameter represents the default email recipient, $fields contains all the form fields and their values, and $form_data contains information about the form being submitted.

Inside the function, we check if the form ID matches the desired form (in this case, form ID 123). Then, we retrieve the value of a specific form field using the wpforms()->process->fields() function, passing the $fields array and the field ID.

Based on the field value, we modify the $recipient variable accordingly. In this example, if the field value is ‘option1’, the recipient will be set to ‘recipient1@example.com’. If the field value is ‘option2’, the recipient will be set to ‘recipient2@example.com’. Otherwise, the default recipient will be ‘default@example.com’.

Finally, we return the modified $recipient value.

You can customize this code snippet by replacing the form ID, field ID, and recipient email addresses to fit your specific use case.

Examples

Example #1: Sending different email notifications based on user input in WP Forms

This use case demonstrates how to send different email notifications based on user input in WP Forms. The code example below shows how to use the wpforms_process_complete hook to conditionally send different email notifications based on the value of a specific form field.

function wpsnippets_wpforms_conditional_notifications( $fields, $entry, $form_data ) {
    // Get the value of the form field
    $field_value = $entry['fields'][FIELD_ID]['value'];

    // Check the value and send different email notifications accordingly
    if ( $field_value === 'Option 1' ) {
        wp_mail( 'email1@example.com', 'Notification for Option 1', 'This is the notification for Option 1.' );
    } elseif ( $field_value === 'Option 2' ) {
        wp_mail( 'email2@example.com', 'Notification for Option 2', 'This is the notification for Option 2.' );
    } else {
        wp_mail( 'default@example.com', 'Default Notification', 'This is the default notification.' );
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_conditional_notifications', 10, 3 );

Explanation: The code uses the wpforms_process_complete hook to trigger the function wpsnippets_wpforms_conditional_notifications when a WP Forms submission is completed. Inside the function, it retrieves the value of a specific form field using the $entry parameter. Based on the field value, different email notifications are sent using the wp_mail() function.

Example #2: Redirecting users to different pages based on form input in WP Forms

This use case demonstrates how to redirect users to different pages based on their input in WP Forms. The code example below shows how to use the wpforms_process_complete hook to conditionally redirect users to different pages based on the value of a specific form field.

function wpsnippets_wpforms_conditional_redirect( $fields, $entry, $form_data ) {
    // Get the value of the form field
    $field_value = $entry['fields'][FIELD_ID]['value'];

    // Check the value and redirect accordingly
    if ( $field_value === 'Option 1' ) {
        wp_redirect( 'https://example.com/page1' );
        exit;
    } elseif ( $field_value === 'Option 2' ) {
        wp_redirect( 'https://example.com/page2' );
        exit;
    } else {
        wp_redirect( 'https://example.com/default' );
        exit;
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_conditional_redirect', 10, 3 );

Explanation: The code uses the wpforms_process_complete hook to trigger the function wpsnippets_wpforms_conditional_redirect when a WP Forms submission is completed. Inside the function, it retrieves the value of a specific form field using the $entry parameter. Based on the field value, users are redirected to different pages using the wp_redirect() function.

Example #3: Updating user roles based on form input in WP Forms

This use case demonstrates how to update user roles based on form input in WP Forms. The code example below shows how to use the wpforms_process_complete hook to conditionally update user roles based on the value of a specific form field.

function wpsnippets_wpforms_conditional_user_roles( $fields, $entry, $form_data ) {
    // Get the value of the form field
    $field_value = $entry['fields'][FIELD_ID]['value'];

    // Check the value and update user roles accordingly
    if ( $field_value === 'Option 1' ) {
        $user = wp_get_current_user();
        $user->set_role( 'subscriber' );
    } elseif ( $field_value === 'Option 2' ) {
        $user = wp_get_current_user();
        $user->set_role( 'contributor' );
    } else {
        $user = wp_get_current_user();
        $user->set_role( 'subscriber' );
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_conditional_user_roles', 10, 3 );

Explanation: The code uses the wpforms_process_complete hook to trigger the function wpsnippets_wpforms_conditional_user_roles when a WP Forms submission is completed. Inside the function, it retrieves the value of a specific form field using the $entry parameter. Based on the field value, the user’s role is updated using the set_role() method of the WP_User class.

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

Leave a Reply

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