在没有插件的情况下上传数千张图片到WordPress?

时间:2018-04-20 作者:Lucas Bustamante

更新:在调试了为什么上传速度这么慢之后,我发现cuscript就是WP-Smush-It插件。下面的问答是找到答案的过程

我正在通过自己编写的自定义导入功能处理数千张上传的图像。它获取一个JSON,从中创建一个帖子,然后处理存储为JSON键的图像URL,并将其上传到WordPress。它工作得很好,但是,上传每张图像大约需要5秒钟。

图像存储在本地,因此WordPress只需移动文件,而不需要下载。

以下是上载图像功能的计时:

  \'uploadtimedebug_start\' => 0 seconds
  \'uploadtimedebug_after_requires\' => 0.006 seconds
  \'uploadtimedebug_after_tmp_filled\' => 0.597 seconds
  \'uploadtimedebug_before_media_handle_sideload\' => 0.597 seconds
  \'uploadtimedebug_after_media_handle_sideload\' => 5.370 seconds // Guilty
  \'uploadtimedebug_finish\' => 5.370 seconds
这里是什么media_handle_sideload 其参数如下:

// Actually uploads the image
$id = media_handle_sideload( $file_array, $post_id, $desc);
以下是的参数media_handle_sideload:

[
    \'file_array\' => [
        \'name\' => \'logo.png\',
        \'tmp_name\' => \'C:\\\\Users\\\\Lucas\\\\AppData\\\\Local\\\\Temp/logo-Rg0xrY.tmp\'
    ],
    \'post_id\' => 881,
    \'desc\' => \'Logo\',
];
进入内部media_handle_sideload 调试时间,有以下内容:

  \'timedebug_start\' => 0 seconds
  \'timedebug_before_wp_handle_sideload\' => 0.001 seconds
  \'timedebug_after_wp_handle_sideload\' => 0.006 seconds
  \'timedebug_before_wp_read_image_metadata\' => 0.006 seconds
  \'timedebug_after_wp_read_image_metadata\' => 0.006 seconds
  \'timedebug_before_wp_insert_attachment\' => 0.006 seconds
  \'timedebug_after_wp_insert_attachment\' => 0.058 seconds
  \'timedebug_before_wp_update_attachment_metadata\' => 0.058 seconds
  \'timedebug_after_wp_update_attachment_metadata\' => 2.635 seconds // Guilty
  \'timedebug_finish\' => 2.635 seconds
所以这种懒散来自wp_update_attachment_metadata. 深入研究,有以下内容:

  \'timedebug_start\' => 0 seconds
  \'timedebug_after_get_post\' => 0 seconds
  \'timedebug_before_apply_filters_wp_update_attachment_metadata\' => 0 seconds
  \'timedebug_after_apply_filters_wp_update_attachment_metadata\' => 3.082 seconds // Guilty
  \'timedebug_before_update_post_meta\' => 3.082 seconds
  \'timedebug_after_update_post_meta\' => 3.104 seconds
所以,最大的懒散来自于:

/**
 * Filters the updated attachment meta data.
 *
 * @since 2.1.0
 *
 * @param array $data          Array of updated attachment meta data.
 * @param int   $attachment_id Attachment post ID.
 */
if ( $data = apply_filters( \'wp_update_attachment_metadata\', $data, $post->ID ) )
    return update_post_meta( $post->ID, \'_wp_attachment_metadata\', $data );
else
    return delete_post_meta( $post->ID, \'_wp_attachment_metadata\' );
我找不到什么add_filter(\'wp_update_attachment_metadata\', $data, $post->ID) 调用以进一步调试它。

无论如何

有没有人知道如何将数千张图片上传到WordPress,同时实时获取他们的附件ID作为回报?

PS:我已经看到了从服务器添加插件,但它不能满足我的需要,因为我必须按ID将图像分配给特定的自定义字段等,所以我需要实时处理这些图像并获得它们的ID

1 个回复
最合适的回答,由SO网友:Lucas Bustamante 整理而成

我修改了方法wp_update_attachment_metadata 从…起wp-includes/post.php (第5070行)在导入期间:

/**
 * Update metadata for an attachment.
 *
 * @since 2.1.0
 *
 * @param int   $attachment_id Attachment post ID.
 * @param array $data          Attachment meta data.
 * @return int|bool False if $post is invalid.
 */
function wp_update_attachment_metadata( $attachment_id, $data ) {
    $attachment_id = (int) $attachment_id;
    if ( ! $post = get_post( $attachment_id ) ) {
        return false;
    }

    /**
     * Filters the updated attachment meta data.
     *
     * @since 2.1.0
     *
     * @param array $data          Array of updated attachment meta data.
     * @param int   $attachment_id Attachment post ID.
     */
     return update_post_meta( $post->ID, \'_wp_attachment_metadata\', $data );
}
媒体上载之前:

  \'uploadtimedebug_start\' => 0 seconds
  \'uploadtimedebug_after_requires\' => 0.006 seconds
  \'uploadtimedebug_after_tmp_filled\' => 0.597 seconds
  \'uploadtimedebug_before_media_handle_sideload\' => 0.597 seconds
  \'uploadtimedebug_after_media_handle_sideload\' => 5.370 seconds
  \'uploadtimedebug_finish\' => 5.370 seconds
媒体上传时间:

  \'uploadtimedebug_inicio\' => 0 seconds
  \'uploadtimedebug_after_requires\' => 0.007 seconds
  \'uploadtimedebug_after_tmp_filled\' => 0.620 seconds
  \'uploadtimedebug_before_media_handle_sideload\' => 0.620 seconds
  \'uploadtimedebug_after_media_handle_sideload\' => 0.723 seconds
  \'uploadtimedebug_finish\' => 0.723 seconds
没有副作用,一切正常。请记住,这应该是一个临时的黑客行为,当你上传完数千张图片后,请还原wp_update_attachment_metadata 到它的original code.

编辑-请忽略我所说的一切

cuscript是一个插件“Smush It”,连接到wp_update_attachment_metadata 滤器禁用Smush It后,过滤器用了零秒。

结束

相关推荐

Get images by category

我目前获取滑块图像的方法是使用以下工作正常的代码:$args = array( \'post_type\' => \'attachment\', \'sort_order\' => \'ASC\', \'sort_column\' => \'menu_order\', ); $attachments = get_posts($args); if ($attachments) { $