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 custom form for posting in my theme file. I also need to upload photos(videos also but photos are more important now) and I prepared form using wp handle upload function. It's working, images are uploading but they're not inserting into the post. Please help me. Here is a code full http://cdpst.net/s8i5jsfuf and here is the relevant bit:

if ( $_POST['question_post'] == '1') {
    $question_title = $_POST['question_title'];
    $question_content = $_POST['question_content'];
    $question_category = $_POST['cat'];
    $question_tags = $_POST['question_tags'];

    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            $newupload = insert_attachment($file,$pid);
            // $newupload returns the attachment id of the file that
            // was just uploaded. Do whatever you want with that now.
        }
    }

    // ...
}
share|improve this question
    
Code is not reachable. Please post the relevant bits here. –  soulseekah Oct 22 '11 at 5:49
    
add comment

2 Answers

Let's presume your FILE variable is called 'files' and can contain multiple inputs (i.e. name="files[]")

1) First upload the files

$uploads = wp_upload_dir();
  $upload_file_url = array();
  foreach ($_FILES['files']['name'] as $key => $value_data)
    {
        $errors_founds = '';

        if ($_FILES['files']['error'][$key] != UPLOAD_ERR_OK)
            $errors_founds .= 'Error uploading the file!<br />';

        if (!array_key_exists($_FILES['files']['type'][$key], $mime))
            $errors_founds .= 'Invalid file type!<br />';

        if ($_FILES['files']['size'][$key] == 0)
            $errors_founds .= 'Image file it\'s empty!<br />';

        if ($_FILES['files']['size'][$key] > 524288)
            $errors_founds .= 'Image file to large, maximus size is 500Kb!<br />';

        if(!is_uploaded_file($_FILES['files']['tmp_name'][$key]))
             $errors_founds .= 'Error uploading the file on the server!<br />';

        if ($errors_founds == '')
            {
                //Sanitize the filename (See note below)
                $remove_these = array(' ','`','"','\'','\\','/');
                $newname = str_replace($remove_these,'', $_FILES['files']['name'][$key]);
                //Make the filename unique
                $newname = time().'-'.$newname;
                //Save the uploaded the file to another location

                $upload_path = $uploads['path'] . "/$newname";
                move_uploaded_file($_FILES['files']['tmp_name'][$key], $upload_path);
                $upload_files_url[$upload_path] = $uploads['url'] ."/$newname";
            }
    }

2) Process the successfully uploaded files

if (count($upload_files_url)  > 0)
    {
        foreach ($upload_files_url as $filename_path => $upload_file_url)
            {
                $wp_filetype = wp_check_filetype(basename($filename_path), null );
            $attachment = array(
                                'post_mime_type' => $wp_filetype['type'],
                                'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename_path)),
                                'post_content' => '',
                                'post_status' => 'inherit'
                                );
            $attach_id = wp_insert_attachment( $attachment, $filename_path);
            // you must first include the image.php file
            // for the function wp_generate_attachment_metadata() to work
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename_path );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            }
    }
share|improve this answer
add comment

First of all it should be noted that your form does not appear to have the required MAX_FILE_SIZE value. Please refer to the POST method uploads section of the PHP manual.

Next, there is no such function as insert_attachment(), the function is wp_insert_attachment(). It requires:

  • an $attachment array with the file attributes, etc.
  • the $filename of the uploaded file (MUST be in the WordPress uploads directory (wp_upload_dir()), so you have to take care of transferring it over, with move_uploaded_file() for example)
  • and the $parent_post_id, which is presumably your current post ID, which you can get by using the get_the_ID() template function.

The Codex page for wp_insert_attachment() has a great example of how to attach files (specifically images) to a post.

share|improve this answer
add comment

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.