0

I have an image upload form that is being added to the_content using a filter. However, the existence of this image upload form makes it so that comments cannot be added. Because there are two submit buttons, the comment submit button seems like it's actually submitting the image upload form.

Here is the form:

<form method="post" action="" enctype="multipart/form-data" >
    <li><input type="file" accept="image/*" name="uploaded_attachment" id="uploaded-attachment"></li>
    <li><input type="submit" value="Upload"></li>
<form>

Is there an easy way to make it so the comment submit only submits comments and this form only submits the image upload? Thanks in advance.

1 Answer 1

0

Well, this is definitely a little hackish but what I ended up doing was changing the action on the form to "[mysite url]/wp-comments-post.php". Then I took advantage of a few action hooks in wp-comments-post.php to hijack that page:

add_action('pre_comment_on_post', array($this, 'process_image_upload'));

And if anyone is curious as to how I wrote the process_image_upload function (which uploads an image), here it is:

private function process_image_upload($comment_post_ID){
    //see which submit button was pressed
    $comment = ( isset($_POST['submit']) )  ? trim(strip_tags($_POST['submit'])) : null;

            //if it wasn't the comment submit button then...
        if($comment != 'Post Comment'){
            if(current_user_can('upload_files')){
                if ($_FILES) {
                    foreach ($_FILES as $file => $array) {
                    $newupload = $this->insert_attachment($file,$comment_post_ID);
                    // $newupload returns the attachment id of the file that was just uploaded. Do whatever you want with that now.
                    }
                    if($newupload){
                        $location = get_permalink( $comment_post_ID );
                        wp_redirect($location);
                        exit;
                    }
                }
            }else {
                wp_die( __('You do not have permission to upload files') );
            }
        }
    }

It's still needs a little work, but this should at least get you started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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