以编程方式将图像添加到媒体库

时间:2017-02-17 作者:Adrian

我试图以编程方式将多个图像添加到媒体库,我将图像上载到wp-content/uploads, 现在我尝试使用wp_insert_attachement.

这是代码,但它没有按预期工作,我认为元数据没有正确生成,我可以看到媒体库中的文件,但没有缩略图,而且如果我编辑图像,我会收到一个错误消息,说要重新上传图像。

$filename_array = array(
   \'article1.jpg\',
   \'article2.jpg\',
);

// The ID of the post this attachment is for.
$parent_post_id = 0;

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

foreach ($filename_array as $filename) {

    // Check the type of file. We\'ll use this as the \'post_mime_type\'.
    $filetype = wp_check_filetype( basename( $filename ), null );

    // Prepare an array of post data for the attachment.
    $attachment = array(
        \'guid\'           => $wp_upload_dir[\'url\'] . \'/\' . basename( $filename ), 
        \'post_mime_type\' => $filetype[\'type\'],
        \'post_title\'     => preg_replace( \'/\\.[^.]+$/\', \'\', basename( $filename ) ),
        \'post_content\'   => \'\',
        \'post_status\'    => \'inherit\'
    );

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

    // Make sure that this file is included, as   wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . \'wp-admin/includes/image.php\' );

    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

}

3 个回复
最合适的回答,由SO网友:TrubinE 整理而成
$image_url = \'adress img\';

$upload_dir = wp_upload_dir();

$image_data = file_get_contents( $image_url );

$filename = basename( $image_url );

if ( wp_mkdir_p( $upload_dir[\'path\'] ) ) {
  $file = $upload_dir[\'path\'] . \'/\' . $filename;
}
else {
  $file = $upload_dir[\'basedir\'] . \'/\' . $filename;
}

file_put_contents( $file, $image_data );

$wp_filetype = wp_check_filetype( $filename, null );

$attachment = array(
  \'post_mime_type\' => $wp_filetype[\'type\'],
  \'post_title\' => sanitize_file_name( $filename ),
  \'post_content\' => \'\',
  \'post_status\' => \'inherit\'
);

$attach_id = wp_insert_attachment( $attachment, $file );
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
SO网友:Paul Schreiber

如果您使用WordPress的侧载功能,您可以更轻松地完成这项工作(并让WordPress为您处理所有的清理)。

<?php
$desc = \'some description\';
$file = \'http://www.example.com/image.png\';
$file_array  = [ \'name\' => wp_basename( $file ), \'tmp_name\' => download_url( $file ) ];

// If error storing temporarily, return the error.
if ( is_wp_error( $file_array[\'tmp_name\'] ) ) {
    return $file_array[\'tmp_name\'];
}

// Do the validation and storage stuff.
$id = media_handle_sideload( $file_array, 0, $desc );

// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) {
    @unlink( $file_array[\'tmp_name\'] );
    return $id;
}

SO网友:Lance Cleveland

我对@TrubinE的解决方案有一些问题,没有加载图像文件。

下面是一个对我有用的完整示例:https://gist.github.com/m1r0/f22d5237ee93bcccb0d9

这是一个类似的想法,但使用WP HTTP库来获取内容,而不是使用file\\u get\\u contents()。以下是来自m1r0的github gist解决方案的内容:

        if( !class_exists( \'WP_Http\' ) )
        include_once( ABSPATH . WPINC . \'/class-http.php\' );
    $http = new WP_Http();
    $response = $http->request( $meta[ \'image_url\' ] );
    if( $response[\'response\'][\'code\'] != 200 ) {
        return false;
    }
    $upload = wp_upload_bits( basename($meta[ \'image_url\' ]), null, $response[\'body\'] );
    if( !empty( $upload[\'error\'] ) ) {
        return false;
    }
    $file_path = $upload[\'file\'];
    $file_name = basename( $file_path );
    $file_type = wp_check_filetype( $file_name, null );
    $attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
    $wp_upload_dir = wp_upload_dir();

    $post_info = array(
        \'guid\'           => $wp_upload_dir[\'url\'] . \'/\' . $file_name,
        \'post_mime_type\' => $file_type[\'type\'],
        \'post_title\'     => $attachment_title,
        \'post_content\'   => \'\',
        \'post_status\'    => \'inherit\',
    );

    $attach_id = wp_insert_attachment( $post_info, $file_path, $parent_post_id );
    require_once( ABSPATH . \'wp-admin/includes/image.php\' );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
    wp_update_attachment_metadata( $attach_id,  $attach_data );
    return $attach_id; code here

相关推荐

Adding Images into API

我正试图将特色图片添加到我的API中,但这是一种不同类型的帖子。http://example.com/wp-json/wp/v2/directory我一直在使用WP REST API控制器,但它没有显示图像选项。我也尝试使用“更好的RESTAPI特色图片”,但运气不好。这开始让我恼火了。