gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:04:02 24 4
gpt4 key购买 nike

我正在尝试构建一个 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;
}
}
}

最佳答案

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;

关于php - 从 URL 下载图像并上传到 WordPress 媒体库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com