Monday 15 February 2010

php - wp_insert_post multi upload Images -


Hi I want to upload several images with wp_insert_post, but uploading multiple images only allows other images in the previous image To upload but not attached

Here is the code

  & lt; Form action = "#" method = "post" enctype = "multipart / form-data" & gt; & Lt; Input type = "file" name = "agp_gallery []" id = "agp_image_files" style = "width: 90%; margin-left: 5px;" Many & gt;   

The function is multipoled here

  if ($ _FILES) {$ files = $ _FILES ['agp_gallery']; ($ Files ['name'] [$ of]) {$ file = array ('name' = & gt; $ files ['name $] [$ Key],' type '= & gt; $ files [' type '] [$ Key],' tmp_name '= & gt; $ files [' tmp_name '] [$ key],' error '= & gt; files [' error '] [$ key],' size '=> ; $ Files ['size'] [$ of]); $ _FILES = array ("AGP_Gallery" => $ file); Forex Currency ($ $ _FILES $ file = & gt; $ as array) {agp_process_wooimage ($ file, $ post_id, $ result ['caption']); Here is the main function  
  function agp_process_wooimage ($ file, $ post_id, $ caption) {if ($ _FILES [$ file] ['Error']! == UPLOAD_ERR_OK) __return_false (); Need_once (ABSPATH. "Wp-admin". '/includes/image.php'); Need_once (ABSPATH. "Wp-admin". '/includes/file.php'); Need_once (ABSPATH. "Wp-admin". '/includes/media.php'); $ Attachment_id = media_handle_upload ($ file, $ post_id); Update_post_meta ($ post_id, '_product_image_gallery', $ attachment_id); $ Attachment_data = array ('id' = & gt; $ attachment_id, 'post_excerpt' = & gt; $ caption); Wp_update_post ($ attachment_data); Return $ attachment_id; }   

I get the code above here: -

and

All codes here: -

please try it

  Function agp_process_wooimage ($ File, $ post_id) {if ($ _FILES [$ file] ['error']! == UPLOAD_ERR_OK) __return_false (); Need_once (ABSPATH. "Wp-admin". '/includes/image.php'); Need_once (ABSPATH. "Wp-admin". '/includes/file.php'); Need_once (ABSPATH. "Wp-admin". '/includes/media.php'); $ Attachment_id = media_handle_upload ($ file, $ post_id); Update_post_meta ($ post_id, array_push ($ post_id, '_product_image_gallery', $ attachment_id)); Return $ attachment_id; }    

No comments:

Post a Comment