如何解决多站点上的 Wordpress 插入图像仅适用于站点 1
我们有一个多站点 Wordpress 站点,我们将一些外部数据与 ftp 目录中的给定图像同步为特色图像。在站点 1 上,这个过程就像一个魅力。 但是在所有其他站点上, wp_insert_attachment 函数缺少一些东西。 我们得到了一个附件 ID,我们可以看到@数据库的帖子数量不断增加,并且找不到手动和以编程方式生成的图像之间的区别,但是: 在wordpress的媒体库中,图片实际上丢失但存在于多站点的上传目录中(例如/wp-content/uploads/sites/2/2021/05)
这里是我们插入附件的方式:
require_once("wp-load.php");
require_once(ABSPATH . 'wp-admin/includes/image.php');
switch_to_blog(2);
$IMGFileName = 'filename123.jpg';
$IMGFilePath = ABSPATH. 'path/to/files/'.$IMGFileName;
// Add Featured Image to Post
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($IMGFilePath); // Get image data
$filename = basename($IMGFilePath); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file,$image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename,null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],'post_title' => sanitize_file_name( $filename ),'post_content' => '','post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment,$file );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id,$file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id,$attach_data );
似乎在某些函数中忽略了 switch_to_blog(2);。 有人知道我们在这里缺少什么吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。