Altering the attachments for E-mails via PHP code for WordPress
If you have a form with a dropdown and you wish to attach a specific file to the E-mails based on the selected dropdown item you can use the below code to achieve this.
add_filter('super_before_sending_email_attachments_filter', 'f4d_custom_attach_file_based_on_dropdown', 10, 2);
function f4d_custom_attach_file_based_on_dropdown($attachments, $atts) {
// Retrieve the form data
$form_data = $atts['data'];
// Replace 'your_dropdown_name' with the actual name or ID of your dropdown field
$dropdown_name = 'your_dropdown_name';
// Ensure the dropdown field exists
if (isset($form_data[$dropdown_name])) {
$dropdown_value = $form_data[$dropdown_name]['value'];
// Define your attachments based on dropdown value
$attachments_map = array(
'option1' => 1234, // ID of the attachment in WP
'option2' => 1235, // ID of the attachment in WP
'option3' => 1236 // ID of the attachment in WP
);
// Check if the selected value has an attachment
if (isset($attachments_map[$dropdown_value])) {
// Add the attachment to the attachments array
$attachments[$dropdown_value.'.pdf'] = wp_get_attachment_url($attachments_map[$dropdown_value]);
}
}
return $attachments;
}PreviousCombine multiple field values into one column on Contact Entries pageNextGenerate dynamic columns with dates based on user selected date from Datepicker element
Last updated