从URL下载图像并上传到WordPress媒体库

问题描述 投票:1回答:1

我正在尝试构建一个WordPress函数,它接受图像URL,下载图像,将其上传到媒体库,然后返回图像ID。

我改编自this answer,这是taken from here,最后我添加了wp_insert_attachment()

目前它不起作用,因为它不返回任何内容或上传任何媒体。

我通过使用var_dump()来完成调试,我发现$url参数正确传递,但download_url没有输出任何内容。

你知道什么是错的吗?

你能看到功能中可能被破坏的其他东西吗?

/* Add image to media library from URL and return the new image ID */
function bg_image_upload($url) {

  // Gives us access to the download_url() and wp_handle_sideload() functions
  require_once( ABSPATH . 'wp-admin/includes/file.php' );

  // Download file to temp dir
  $timeout_seconds = 10;
  $temp_file = download_url( $url, $timeout_seconds );

  if ( !is_wp_error( $temp_file ) ) {

      // Array based on $_FILE as seen in PHP file uploads
      $file = array(
          'name'     => basename($url), // ex: wp-header-logo.png
          'type'     => 'image/png',
          'tmp_name' => $temp_file,
          'error'    => 0,
          'size'     => filesize($temp_file),
      );

      $overrides = array(
          // Tells WordPress to not look for the POST form
          // fields that would normally be present as
          // we downloaded the file from a remote server, so there
          // will be no form fields
          // Default is true
          'test_form' => false,

          // Setting this to false lets WordPress allow empty files, not recommended
          // Default is true
          'test_size' => true,
      );

      // Move the temporary file into the uploads directory
      $results = wp_handle_sideload( $file, $overrides );

      if ( !empty( $results['error'] ) ) {
          // Insert any error handling here
      } else {

          $filename  = $results['file']; // Full path to the file
          $local_url = $results['url'];  // URL to the file in the uploads dir
          $type = $results['type']; // MIME type of the file
          $wp_upload_dir = wp_upload_dir(); // Get the path to the upload directory.

          $attachment = array (
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
            'post_mime_type' => $type,
            'post_status' => 'inherit',
            'post_content' => '',
          );

          $img_id = wp_insert_attachment( $attachment, $filename  );

          return $img_id;
      }
  }
}
php wordpress
1个回答
0
投票

100%使用此代码

include_once( ABSPATH . 'wp-admin/includes/image.php' );
$imageurl = '<IMAGE URL>';
$imagetype = end(explode('/', getimagesize($imageurl)['mime']));
$uniq_name = date('dmY').''.(int) microtime(true); 
$filename = $uniq_name.'.'.$imagetype;

$uploaddir = wp_upload_dir();
$uploadfile = $uploaddir['path'] . '/' . $filename;
$contents= file_get_contents($imageurl);
$savefile = fopen($uploadfile, 'w');
fwrite($savefile, $contents);
fclose($savefile);

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

$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$imagenew = get_post( $attach_id );
$fullsizepath = get_attached_file( $imagenew->ID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data ); 

echo $attach_id;
© www.soinside.com 2019 - 2024. All rights reserved.