doujiong3146 2018-09-23 10:11
浏览 59

为WP制作一个maxi-Javascript文件上传器

I come from Object Oriented PHP and Laravel, and now I'm building (my first) plugin for wordpress. The plugin it's already working with a basic upload function that allow the user to upload multiple files (photos) from the disk, and create a wp-post (kind: attachment) from them, attaching some other infos:

  $files = $_FILES["my_file_upload"];

      foreach ($files['name'] as $key => $value) {
          if ($files['name'][$key]) {
              $file = array(
                  'name' => $files['name'][$key],
                  'type' => $files['type'][$key],
                  'tmp_name' => $files['tmp_name'][$key],
                  'error' => $files['error'][$key],
                  'size' => $files['size'][$key]
              );
              $_FILES = array("upload_file" => $file);
              $attachment_id = media_handle_upload("upload_file", 0);

              if (is_wp_error($attachment_id)) {
                  // There was an error uploading the image.
                  echo "Error adding file";

              } else {

                // The image was uploaded successfully!
                      //Take the album id from the URL
                      if ($_GET['album_id'] != ''){

                          wp_update_post(
                          array(
                              'ID' => $attachment_id,
                              'post_parent' => $_GET['album_id']
                                  )
                              );
                          echo 'id album'.$_GET['album_id'];
                        }

Everythings works fine. The problem is that if the user upload to many files of course there is the possibility of running out of memory.

What I'd like to do:

I want to make a script (I'm good in vue, but I can do it also in jQuery or in pure js) that after the user has selected all the files that he wants to upload, makes an array of this files, and then through an AJAX request, it add one file per time (per each request), of course showing to the user adding file 1 of x--- and so on. I already worked (last week) with AJAX in Wp, at the begin it was a bit painful (I didn't know at all that all the ajax requests in WP have to pass form the native WP AJAX handler ) but I sorted it out.

I think to start with something like:

   var x = document.getElementById("my_file_upload");
if ('files' in x) {
    if (x.files.length == 0) {
        txt = "No file selected";
    } else {
        for (var i = 0; i < x.files.length; i++) {


  // make an ajax request here for the first file,
   // then when get the answer, take away the first file from the array and do 
//the same to the second and so on... 

As said I'm good in php but pretty new to code in WP, so I'm asking if somebody has made something similiar and have any suggestion that can be usefull to save time.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题