[wp-hackers] Allow users to upload their own images - hook to use?

J.D. Grimes jdg at codesymphony.co
Thu Sep 12 18:46:51 UTC 2013


> Yea a couple people on stack overflow told me the same about
> insert_attachment returning the post ID of the attachment, that I dont
> deny.  I just dont see how I can post it with the other form data.  Only
> way I can think is a session.  I tried a variable within the value of the
> input field but obviously that failed immediately since it has no value
> until after the file is uploaded.

Well, I'm not sure I understand what the problem is. Both the attachment and your data are being inserted on form submit, as per the code you posted. And the attachment is inserted before your custom data. So there is no problem there. Get the ID from the $newupload variable:

> ///SUBMIT FORM CODE
> 
> $post_id = NULL;
> global $wpdb;
> if ($_FILES) {
>  foreach ($_FILES as $key => $value) {
>    $newupload = insert_attachment($file, $post_id);
>    /****** $newupload returns the attachment id of the file that *****/
>    // was just uploaded. Do whatever you want with that now.
> 
> 
> if (isset($_POST['submit'])) {
> $wpdb->insert(wp_jo_plugin_options, array (
>    'user_id' => $_POST['user_id'],
>    'length'  => $_POST['length'],
>    'ground'  => $_POST['ground'],
>    'date'    => $_POST['date'],
> 'file' => $_FILES['file'],
> ));
>        }
>    }
> }



More information about the wp-hackers mailing list