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>

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


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 ($_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.
                        $location = get_permalink( $comment_post_ID );
            }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 you have read our privacy policy.

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