我的多站点设置有一种管理站点,用户可以在其中插入/编辑帖子,并指定要在哪个站点上发布帖子。这是合同要求,不能更改。
EDIT: Check the accepted answer for a much simpler approach to this.
switch_to_blog()
但我们在上传文件时遇到了一些不适。我们决定使用WP上传器,因此,将管理媒体库作为上传文件的沙盒。所以过程是这样的:
客户端在表单上插入新帖子和图像。
WP Uploader处理上传、处理,并将文件临时放置在管理站点媒体库中,自定义值表示其用户ID和会话ID(目前我们还没有帖子ID)。
用户单击保存帖子。
系统火灾wp_insert_post()
并获取返回的帖子ID和:
<?php
// functions.php inside the function triggered upon post save
foreach ($files as $f) {
restore_current_blog();
/*
* 5. System copies the files from the Admin Site Uploads Dir to the Selected Site Uploads Dir.
*/
$filename = get_attached_file($f);
$arq = get_post($f, ARRAY_A);
$filedest = str_replace(\'blogs.dir/22/\', \'blogs.dir/\' . $marca . \'/\', $filename);
if (copy($filename, $filedest)) {
switch_to_blog($marca);
unset($arq[\'ID\']);
/*
* 6. System fires `wp_insert_attachment()`, `wp_generate_attachment_metadata()`
* and `wp_update_attachment_metadata()` associating the newly moved files to the
* returned Post ID from step 4.
*
*/
$att = wp_insert_attachment($arq, $filedest, $err);
if ($att) {
$attach_data = wp_generate_attachment_metadata( $att, $filedest );
wp_update_attachment_metadata( $att, $attach_data );
if ($f == $destaque) {
update_post_meta($err, \'_thumbnail_id\', $att);
}
restore_current_blog();
/*
* 7. System deletes the old images from the Admin Media Library aka Sandbox.
*/
$del = wp_delete_attachment( $f, true );
$msg = \'8. User is (should be) happy his files are properly put in place and goes on with his life.\';
} else {
echo \'erro\';
}
}
}
?>
将文件复制到正确的路径并正确管理附件后,我们发现尽管主文件已存在并已附加到正确的帖子,但我们正在调用wp_generate_attachment_metadata
和wp_update_attachment_metadata
在…内switch_to_blog()
没有生成在目标站点上定义的中间大小,因此没有连接到的函数add_attachment
. 我怀疑这与管理站点功能触发的操作有关。无法访问其他网站的功能。
那么,有没有更聪明的方法来移动这些图像,就像它们是目标站点的本地图像一样?