Site icon Hip-Hop Website Design and Development

Assist to add publish attachments from Ajax

My drawback is that I can’t add publish attachments from a frontend type. I ship information via AJAX to my php recordsdata containing features. Let me be clear: all the things is working nice, the publish is created with all informations. The one lacking factor is that the photographs I despatched are usually not connected to the publish.

My drawback ought to be in my php operate to connect pictures.

My html

<enter id="moreimages" sort="file" title="moreimages[]" a number of >

My php operate

$pid = // venture id
$uid = // present person

if ( $_FILES ) { 
$recordsdata = $_FILES["moreimages"];  
foreach ($recordsdata['name'] as $key => $worth) {            
            if ($recordsdata['name'][$key]) {
                $upload_overrides             = array('test_form' => false);
                $uploaded_file                = wp_handle_upload($_FILES['file'], $upload_overrides);
                $file_name_and_location       = $uploaded_file['file'];
                $file_title_for_media_library = $_FILES['file']['name'];
                $arr_file_type      = wp_check_filetype(basename($_FILES['file']['name']));
                $uploaded_file_type = $arr_file_type['type'];
                $picture = array( 
                    'post_mime_type' => $uploaded_file_type,
                    'post_title' => addslashes($file_title_for_media_library),
                    'post_content' => '',
                    'post_status' => 'inherit',
                    'post_parent' => $pid,
                    'post_author' => $uid
                ); 
                $_FILES = array ("moreimages" => $picture); 
                foreach ($_FILES as $pictures => $array) {              
                    $image_id = wp_insert_attachment($picture, $file_name_and_location, $pid);
                    $attach_data = wp_generate_attachment_metadata($image_id, $file_name_and_location);
                    wp_update_attachment_metadata($image_id, $attach_data); 
                }
            } 
        } 
    }

I repeat I see in my console.log that pictures are accurately despatched by AJAX into FormData object. I see this after I add FIRST.jpg.

Content material-Disposition: form-data; title="moreimages[]"; filename="FIRST.jpg"
Content material-Sort: picture/jpeg

So the issue ought to be within the php loop for attaching pictures. May you assist me please? Thanks prematurely!