wsf_action_email_to

Description

The wsf_action_email_to filter allows you to modify the To array of email addresses when sending emails using the Send Email action.

Usage

add_filter( 'wsf_action_email_to', 'my_hook_function', 10, 4 );

Parameters

  1. $to Array
    An array of email addresses in RFC 2822 format to send the email to.
  2. $form Form Object
    The form object.
  3. $submit String
    The submit object.
  4. $action Array
    The action configuration.

Example

// Callback function for the wsf_action_email_to filter hook
function my_hook_function( $to, $form, $submit, $action ) {

    // ID of form this filter hook applies to
    $form_id = 123;

    // ID of field
    $field_id = 123;

    // Check form ID
    if( $form_id != $form->id ) { return $to; }

    // Build meta key containing the field value
    $meta_key = sprintf( 'field_%u', $field_id );

    // Get field value (Will be an array of values for a select, checkbox or radio field)
    $values = wsf_submit_get_value( $submit, $meta_key );

    // Check field value
    if( empty( $values ) || !is_array( $values ) ) { return $to; }

    // Field value to email address lookup table (Build this data using your own code)
    $to_lookups = array(

        'Sales' => 'Sales <sales@mysite.com>',
        'Support' => 'Support <support@mysite.com>'
    );

    // Return array
    $to_array = array();

    // Process values
    foreach( $values as $value ) {

        // Check if lookup exists
        if( isset( $to_lookups[$value] ) ) {

            // If it does exist, add this email address to the return array
            $to_array[] = $to_lookups[$value];
        }
    }

    // Return value
    return count( $to_array ) ? $to_array : $to;
}

// Add a callback function for the wsf_action_email_to filter hook
add_filter( 'wsf_action_email_to', 'my_hook_function', 10, 4 );

Source File

This hook can be found in: <plugin root>/includes/actions/class-ws-form-action-email.php