Last updated on October 18, 2023

WP Forms conditional email recipients

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

Set dynamic email recipients in WP Forms.

The code snippet below demonstrates how to set conditional email recipients for WP Forms in WordPress. This functionality can be useful when you want to send form submissions to different email addresses based on certain conditions.

/**
 * Set conditional email recipients for WP Forms.
 *
 * @param array $recipients The default email recipients.
 * @param array $form_data The form data.
 * @return array The updated email recipients.
 */
function wpsnippets_set_conditional_email_recipients( $recipients, $form_data ) {
    // Check if the form ID matches the desired form.
    if ( $form_data['id'] === 123 ) {
        // Check the value of a specific form field.
        $field_value = $form_data['fields']['field_name']['value'];

        // Set different email recipients based on the field value.
        if ( $field_value === 'option1' ) {
            $recipients = array(
                'email1@example.com',
                'email2@example.com',
            );
        } elseif ( $field_value === 'option2' ) {
            $recipients = array(
                'email3@example.com',
                'email4@example.com',
            );
        }
    }

    return $recipients;
}
add_filter( 'wpforms_mail_recipients', 'wpsnippets_set_conditional_email_recipients', 10, 2 );

In this example, we define a custom function wpsnippets_set_conditional_email_recipients that takes two parameters: $recipients (the default email recipients) and $form_data (the form data submitted by the user).

Inside the function, we first check if the form ID matches the desired form. If it does, we retrieve the value of a specific form field using $form_data['fields']['field_name']['value'].

Based on the field value, we update the $recipients array with different email addresses. In this case, if the field value is ‘option1’, the recipients will be set to email1@example.com and email2@example.com. If the field value is ‘option2’, the recipients will be set to email3@example.com and email4@example.com.

Finally, we return the updated $recipients array.

To use this code snippet, replace 123 with the ID of your desired form and 'field_name' with the name or ID of the form field you want to use for conditional email recipients. Update the email addresses as per your requirements.

Examples

Example 1: Conditional Email Recipients based on Form Field Value

This use case demonstrates how to set conditional email recipients based on the value of a form field in WP Forms.

function wpsnippets_conditional_email_recipients( $recipient, $form_data ) {
    // Get the value of the form field
    $field_value = $form_data['fields'][0]['value'];

    // Set the default recipient
    $recipient = 'default@example.com';

    // Check the form field value and update the recipient accordingly
    if ( $field_value === 'Option 1' ) {
        $recipient = 'option1@example.com';
    } elseif ( $field_value === 'Option 2' ) {
        $recipient = 'option2@example.com';
    }

    return $recipient;
}
add_filter( 'wpforms_mailchimp_conditional_email_recipients', 'wpsnippets_conditional_email_recipients', 10, 2 );

In this code example, we define a custom function wpsnippets_conditional_email_recipients that takes two parameters: $recipient and $form_data. We retrieve the value of the form field from $form_data and set a default recipient. Then, we check the value of the form field and update the recipient accordingly. Finally, we return the updated recipient value.

Example 2: Conditional Email Recipients based on Multiple Form Fields

This use case demonstrates how to set conditional email recipients based on the values of multiple form fields in WP Forms.

function wpsnippets_conditional_email_recipients( $recipient, $form_data ) {
    // Get the values of the form fields
    $field1_value = $form_data['fields'][0]['value'];
    $field2_value = $form_data['fields'][1]['value'];

    // Set the default recipient
    $recipient = 'default@example.com';

    // Check the form field values and update the recipient accordingly
    if ( $field1_value === 'Option 1' && $field2_value === 'Option A' ) {
        $recipient = 'option1a@example.com';
    } elseif ( $field1_value === 'Option 2' && $field2_value === 'Option B' ) {
        $recipient = 'option2b@example.com';
    }

    return $recipient;
}
add_filter( 'wpforms_mailchimp_conditional_email_recipients', 'wpsnippets_conditional_email_recipients', 10, 2 );

In this code example, we extend the previous example to handle multiple form fields. We retrieve the values of the form fields from $form_data and set a default recipient. Then, we check the values of the form fields and update the recipient accordingly. Finally, we return the updated recipient value.

Example 3: Conditional Email Recipients based on User Role

This use case demonstrates how to set conditional email recipients based on the user role of the form submitter in WP Forms.

function wpsnippets_conditional_email_recipients( $recipient, $form_data ) {
    // Get the user ID of the form submitter
    $user_id = get_current_user_id();

    // Set the default recipient
    $recipient = 'default@example.com';

    // Check the user role and update the recipient accordingly
    if ( user_can( $user_id, 'administrator' ) ) {
        $recipient = 'admin@example.com';
    } elseif ( user_can( $user_id, 'editor' ) ) {
        $recipient = 'editor@example.com';
    }

    return $recipient;
}
add_filter( 'wpforms_mailchimp_conditional_email_recipients', 'wpsnippets_conditional_email_recipients', 10, 2 );

In this code example, we retrieve the user ID of the form submitter using get_current_user_id(). We set a default recipient and then check the user role using user_can(). Based on the user role, we update the recipient accordingly. Finally, we return the updated recipient value.

Last updated on October 18, 2023. Originally posted on January 4, 2024.

Leave a Reply

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