Advertisement
Guest User

Gérer des fichiers avec wp_insert_post

a guest
Jan 11th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Une fonction helper pour gérer l'upload et la création du media attachment
  5.  */
  6.  
  7. function msk_insert_attachment($file_handler, $id) {
  8.     if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
  9.  
  10.     require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  11.     require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  12.     require_once(ABSPATH . "wp-admin" . '/includes/media.php');
  13.  
  14.     $attach_id = media_handle_upload($file_handler, $id);
  15.  
  16.     return $attach_id;
  17. }
  18.  
  19.  
  20. /*
  21.  * Le traitement des input[type=file] (multiple upload de fichiers possibles)
  22.  * Dans le formulaire il faut : <input type="file" name="fileupload[]" id="fileupload" multiple />
  23.  */
  24.  
  25. if ($_FILES) {
  26.     $files = $_FILES['fileupload'];
  27.  
  28.     foreach ($files['name'] as $key => $value) {
  29.         if ($files['name'][$key]) {
  30.             $file = array(
  31.                 'name'     => $files['name'][$key],
  32.                 'type'     => $files['type'][$key],
  33.                 'tmp_name' => $files['tmp_name'][$key],
  34.                 'error'    => $files['error'][$key],
  35.                 'size'     => $files['size'][$key]
  36.             );
  37.  
  38.             $_FILES = array('upload_attachment' => $file);
  39.  
  40.             foreach ($_FILES as $file => $array) {
  41.                 $new_upload = msk_insert_attachment($file, $product_name_draft_id);
  42.                 $new_array = get_post_meta($product_name_draft_id, '_images_image', true);
  43.                 $new_array[] = $new_upload;
  44.                 update_post_meta($product_name_draft_id, '_images_image', $new_array);
  45.             }
  46.         }
  47.     }
  48. }
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement