Last active
September 10, 2021 08:51
-
-
Save rafiahmedd/11087c38c49d8a20dedb781fba6dc9ee to your computer and use it in GitHub Desktop.
EmailAttachmentfromAExternalLink.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('fluentform_filter_email_attachments', function($emailAttachments, $notification,$form, $submittedData){ | |
$target_form_id = 38; | |
if($form->id != $target_form_id){ | |
return; | |
} | |
$url = "https://www.australianshiatsucollege.com.au/documents/Prospectus.pdf"; | |
$tmp = download_url($url); | |
if (is_wp_error($tmp)) { | |
return 'Error while downloading'; | |
} | |
$post_id = 0; // set 0 for no parent post id or simple attachment otherwise pass post id for include in post | |
$desc = "Your Pdf"; | |
$file_array = array(); | |
// Set variables for storage | |
// fix file filename for query strings | |
preg_match('/[^?]+.(jpg|jpe|jpeg|gif|png|pdf)/i', $url, $matches); | |
$file_array['name'] = basename($matches[0]); | |
$file_array['tmp_name'] = $tmp; | |
// If error storing temporarily, unlink | |
if (is_wp_error($tmp)) { | |
@unlink($file_array['tmp_name']); | |
$file_array['tmp_name'] = ''; | |
} | |
// do the validation and storage stuff | |
$id = media_handle_sideload($file_array, $post_id, $desc); | |
// If error storing permanently, unlink | |
if (is_wp_error($id)) { | |
@unlink($file_array['tmp_name']); | |
return $id; | |
} | |
$src = wp_get_attachment_url($id); | |
$emailAttachments[] = get_attached_file($id); | |
return $emailAttachments; | |
}, 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment