0

I have an email set up using wp_mail that forwards my front-end submission data as well as posting it to the dashboard. The part that I am having trouble with is forwarding the attachment file with that email. I found this post helpful: File upload from front-end form (as attachment) not working

I can only get the email to forward a url of the image OR post it to the dashboard, but not both. I really need it to do both. Problem is $movefile does just that, moves it from the dashboard to the email. Is there a way to do both-keep it in the dashboard and forward with that email?

    //successfully uploads file to dashboard
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file, $pid);
    }


    //successfully sends file url 
    if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
        $uploadedfile = $_FILES['file'];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
    if ( $movefile ) {
        echo "File is valid, and was successfully uploaded.\n";
        var_dump( $movefile);
    } else {
      echo "Possible file upload attack!\n";
    } 

}
0

In case anyone needs this, here is my solution:

if ($_FILES) {

    function insert_attachment($file_handler, $post_id, $setthumb = 'false') {
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attach_id = media_handle_upload( $file_handler, $post_id );

    //get url
    $attachment_url = wp_get_attachment_url($attach_id);
    add_post_meta($post_id, '_file_paths', $attachment_url);

    $attachment_data = array(
    'ID' => $attach_id
    );

    wp_update_post($attachment_data);


    if ($setthumb) update_post_meta($post_id, '_thumbnail_id', $attach_id);
    return $attach_id;

}

    foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file, $pid);
        //getting the url for the new upload
        $attachment_url = wp_get_attachment_url($newupload);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.