Upload images with comment

EDIT: I finally got the right answer; see my own answer beneath this post for everyone that’s interested.

After searching for a couple of days and trying over and over again I really got stuck.
I’ve a client who collects customer experiences on his website by using the comment form; all comments are displayed as a customer review. So far so good.

But since it’s company does a lot with traveling, he needs to give his customers to upload up to five images with there review.

So what I need to do is find a way to let customers upload media along with there filled in comment form. I do know it’s kind of risky, since there could be corrupt images uploaded and so on. But still, I would like to achieve it.

Tried a lot a things but the point where I get stuck over and over again is the file upload handling, for a non-logged-in user which is posted with a comment form…

Any thoughts will be very appreciated!

1 Answer
1

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