O
O
on_click2020-05-05 00:42:40
PHP
on_click, 2020-05-05 00:42:40

Is it possible to speed up the process of uploading images to the vp media library?

Greetings, I found this method in the open spaces.
Here we load the image via media_handle_sideload by URL and return the media file id

. The problem is that you need to pass + -200 images through this method, all this is very slow. I take a time out. Is it possible to somehow "batch" download media files by url?

I would appreciate any hint, thanks.

public function my_sideload_image($file, $post_id, $desc = null ){
    global $debug; // определяется за пределами функции как true

    if( ! function_exists('media_handle_sideload') ) {
      require_once ABSPATH . 'wp-admin/includes/image.php';
      require_once ABSPATH . 'wp-admin/includes/file.php';
      require_once ABSPATH . 'wp-admin/includes/media.php';
    }

    // Загружаем файл во временную директорию
    $tmp = download_url( $file );

    // Устанавливаем переменные для размещения
    $file_array = [
      'name'     => basename( $file ),
      'tmp_name' => $tmp
    ];

    // Удаляем временный файл, при ошибке
    if ( is_wp_error( $tmp ) ) {
      $file_array['tmp_name'] = '';
      if( $debug ) echo 'Tmp file is not exist! <br />';
    }

    // проверки при дебаге
    if( $debug ){
      echo 'File array: <br />';
      var_dump( $file_array );
      echo '<br /> Post id: ' . $post_id . '<br />';
    }

    $id = media_handle_sideload( $file_array, $post_id, $desc );

    // Проверяем работу функции
    if ( is_wp_error( $id ) ) {
      var_dump( $id->get_error_messages() );
    }

    // удалим временный файл
    @unlink( $tmp );

    return $id;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-05-05
@azerphoenix

Hello!
What about these methods:
1) upload your media files via ftp and then import them into the
Media from FTP library - https://wordpress.org/plugins/media-from-ftp/
2) upload your files via ftp or via ftps , and then using wp cli import them into the library
https://developer.wordpress.org/cli/commands/media...
3) Using the REST API.
Similarly, you upload your media files using the protocol available to you, and then create media using the REST API.
For example, https://gist.github.com/ahmadawais/0ccb8a32ea795ff...
I worked with REST APİ in Java and I will say that everything works great there. With rnr, the code from the git above will probably work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question