使用wp_insert_attachment时媒体文件路径出现网站URL重复问题
问题描述
我通过wp_insert_post程序化创建WordPress文章,设置特色图片时使用完整HTTPS远程URL(格式如https://XXXX.com/content/uploads/2023/08/002-2.jpg,后续会动态生成,必须保留完整路径)。但调用wp_insert_attachment保存媒体时,出现了URL重复的问题,生成的文件URL示例:https://XXXX.com/content/uploads/https://XXXX.com/content/uploads/2023/08/002-2.jpg
以下是我的代码:
// Create Post Content $wpdb->query( $query ); if ( !$wpdb->num_rows ) { $post_id = wp_insert_post( array( 'author' => $current_user_id, 'post_title' => 'test', 'post_content' => 'test content...', 'post_status' => 'draft', 'post_type' => 'post' ) ); $image = "https://********.com/content/uploads/2023/08/002-2.jpg"; $attachment_file_type = wp_check_filetype(basename($image), null); $wp_upload_dir = wp_upload_dir(); $attachment_args = array( 'guid' => $wp_upload_dir['url'] . '/' . basename($image), 'post_title' => preg_replace('/\.[^.]+$/', '', basename($image)), 'post_mime_type' => $attachment_file_type['type'] ); $attachment_id = wp_insert_attachment($attachment_args, $image, $post_id); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attachment_meta_data = wp_generate_attachment_metadata($attachment_id, $image); wp_update_attachment_metadata($attachment_id, $attachment_meta_data); set_post_thumbnail($post_id, $attachment_id); }
问题原因
- 参数类型不匹配:
wp_insert_attachment和wp_generate_attachment_metadata的第二个参数需要传入本地服务器的文件路径,但你直接传入了远程图片的HTTPS URL。WordPress会将这个URL当作文件名拼接到本地上传目录后,导致生成的URL出现重复。 - GUID手动拼接错误:你手动将本地上传目录URL与图片文件名拼接作为
guid,但处理远程图片时,WordPress会自动处理文件存储路径和GUID,手动拼接反而会干扰默认逻辑。
修复方案
先将远程图片下载到WordPress本地上传目录,再使用本地路径创建附件。修改后的代码如下:
// Create Post Content $wpdb->query( $query ); if ( !$wpdb->num_rows ) { $post_id = wp_insert_post( array( 'author' => $current_user_id, 'post_title' => 'test', 'post_content' => 'test content...', 'post_status' => 'draft', 'post_type' => 'post' ) ); $image_url = "https://********.com/content/uploads/2023/08/002-2.jpg"; // 下载远程图片 $response = wp_remote_get($image_url); if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { // 处理下载失败的情况 error_log('远程图片下载失败: ' . $image_url); return; } $image_content = wp_remote_retrieve_body($response); $wp_upload_dir = wp_upload_dir(); $image_filename = basename($image_url); // 生成本地存储路径 $local_file_path = $wp_upload_dir['path'] . '/' . $image_filename; // 将图片内容写入本地文件 file_put_contents($local_file_path, $image_content); // 获取文件类型 $attachment_file_type = wp_check_filetype($image_filename, null); $attachment_args = array( 'post_title' => preg_replace('/\.[^.]+$/', '', $image_filename), 'post_mime_type' => $attachment_file_type['type'], 'post_status' => 'inherit' ); // 使用本地文件路径创建附件 $attachment_id = wp_insert_attachment($attachment_args, $local_file_path, $post_id); if (!is_wp_error($attachment_id)) { require_once(ABSPATH . 'wp-admin/includes/image.php'); // 生成附件元数据(使用本地路径) $attachment_meta_data = wp_generate_attachment_metadata($attachment_id, $local_file_path); wp_update_attachment_metadata($attachment_id, $attachment_meta_data); set_post_thumbnail($post_id, $attachment_id); } else { error_log('附件创建失败: ' . $attachment_id->get_error_message()); } }
关键修改点说明
- 新增远程图片下载逻辑,使用
wp_remote_get获取图片内容并写入本地上传目录 - 调用
wp_insert_attachment和wp_generate_attachment_metadata时,传入本地文件路径而非远程URL - 移除手动拼接的
guid参数,让WordPress自动生成正确的GUID - 增加错误处理,捕获图片下载和附件创建失败的情况
内容的提问来源于stack exchange,提问作者DSmit
相关产品推荐
相关产品推荐

