Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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