Wordpress Media库图像没有文件大小

时间:2019-06-26 12:46:05

标签: php xml wordpress simplepie

我有两个网站,网站A和网站B(都运行WordPress)。我正在开发一个插件,该插件将通过RSS feed将来自网站A的帖子数据导入网站B,并使用该数据创建一个新帖子。到目前为止,一切工作正常。然而。由于某些原因,特色图片无法完全导入。它确实附加在帖子上(尽管不显示),并且也存在于媒体库中,但是不显示预览,而且我还注意到“文件大小”数据为空。我在localhost安装上测试了相同的代码,并且工作正常。图片已上传并显示正常。仅当我在实时域上运行该映像时,该映像才起作用。

两个站点都运行PHP 7.2,并安装了最新的WordPress 5.2.2。 最初,特色图片是通过静态网址创建的,但现在我已经对其进行了更改,以便从Feed中的商品附件中获取图片。

// Assign featured image to post// Add featured image to post
$image_url      = $enclosure->get_link();
$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, $post_id);
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);
set_post_thumbnail($post_id, $attach_id);

0 个答案:

没有答案