多站点上的Wordpress插入图像仅适用于站点1



我们有一个多站点Wordpress站点,在那里我们将一些外部数据与ftp目录中的给定图像同步为特色图像。在网站1上,这个过程就像一个魔术。但在所有其他站点上,wpinsert_attachment函数都缺少一些东西。我们得到了一个附件id,我们可以看到@database的帖子数量在增加,并且找不到手动和编程生成的图像之间的区别,但是:在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(在某些函数中被忽略。有人知道吗,我们这里缺少什么?

不是一个解决方案,但这可能会提供线索。我们遇到了与媒体上传有关的类似问题;插入帖子";按钮它在我们的多站点设置的站点1上运行良好,但在之后添加的任何其他站点上都没有。

经过大量挖掘,我意识到在站点1(以及在正常的单个站点上(;插入帖子";返回包装在锚点标记中的img元素,例如'<a href="#"><img src="url"></a>'

而在站点2和多站点上的后续站点上;插入帖子";按钮只返回img元素,即'<img src="url">'

在我们的例子中,用于获取src属性的jQuery假设img元素被包装在另一个元素中,这就是为什么它对我们不起作用

这可能与你的问题完全无关,但我在搜索我们的问题时发现了你的问题,所以它可能会帮助其他人。

相关内容

最新更新