Upload images with comment

EDIT: With some help of a friend I came up with a solution. For everyone interested:
Use a custom post-type, in my case comment_post. Then upload the images like this:

$new_post = array(
'post_title'    => $title,
'post_content'  => $comment,
    'post_status'   => 'pending',// Choose: publish, preview, future, draft, etc.
    'post_type' => 'comments_post'  // Use a custom post type
);
//save the new post and return its ID
$pid = wp_insert_post($new_post); 
//Upload the file(s)
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

if ($_FILES) {
    foreach ($_FILES as $file => $array) {
    //Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)
    if(isset($_FILES[$file]) && $_FILES[$file]['size']>0){
    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
    echo '<div class="allert alert-error"><p>Upload error : ' . $_FILES[$file]['error'] . '</p></div>';
    $upload = false;
}else{
$upload = true;
}
if($upload == true){
    $attach_id = media_handle_upload( $file, $pid );
}
}
 }   
                                }//End if '$_FILES'

                            }//End if errornumbers

Leave a Comment