我正在尝试使用 WP All Export/Import 将 WordPress 安装的部分内容从实时站点传输到我的测试站点。 我不需要转移整个网站,只需转移带有某些自定义字段的某些页面/帖子即可进行测试。 WPBakery Page Builder 中的“单个图像”元素在导入后给我带来了麻烦。图像上传到测试站点后,图像 ID 保留 xml 文件中的值,而不是将图像 ID 更新为新的 ID 值。
这是一张显示正在发生的事情的图像。导入后,图像 ID 14821、14819、14820 等不会更新为新值。 图像 ID 未更新
每张图片都会正确上传到网站(使用新的图片 ID),但帖子中的图片 ID 不会更改。
这是 xml 数据的示例。 注意:[] 包围的值表示出于安全目的,信息已更改。
<?xml version="1.0" encoding="UTF-8"?>
<data>
<post>
<ID>23103</ID>
<Title>App</Title>
<Content><![CDATA[[vc_row][vc_column][vc_single_image image="23106" img_size="large" alignment="center"][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]Lorem ipsum dolor sit amet adipiscing enim sodales magna porta enim vulputate nec quisque. Velit duis enim tellus convallis nisi senectus urna est augue hendrerit maecenas phasellus mauris. Augue ultricies tempor egestas fringilla dui aliquet urna etiam tellus. Hendrerit vel ullamcorper bibendum cursus elementum imperdiet rhoncus lobortis pellentesque risus et a. Cursus imperdiet pretium a consequat eleifend enim bibendum viverra facilisis gravida enim.[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][vc_single_image image="23107" img_size="large" alignment="center"][/vc_column][/vc_row][vc_row][vc_column][vc_single_image image="23105" img_size="large" alignment="center"][/vc_column][/vc_row][vc_row][vc_column][vc_single_image image="23104" img_size="large" alignment="center"][/vc_column][/vc_row]]]></Content>
<Excerpt></Excerpt>
<Date>2023-11-14 18:16:29</Date>
<PostType>page</PostType>
<Permalink>[Permalink]</Permalink>
<ImageURL>[ImageURL]</ImageURL>
<ImageFilename>295077732_420220533480521_5668096374719189352_n.jpg|3.png|2.png|1-1.png</ImageFilename>
<ImagePath>[ImagePath]</ImagePath>
<ImageID>23106|23104|23105|23107</ImageID>
<ImageTitle>295077732_420220533480521_5668096374719189352_n|3|2|1-1</ImageTitle>
<ImageCaption>|||</ImageCaption>
<ImageDescription>|||</ImageDescription>
<ImageAltText>|||</ImageAltText>
<ImageFeatured>[ImageFeatured]</ImageFeatured>
<AttachmentURL></AttachmentURL>
<AttachmentFilename></AttachmentFilename>
<AttachmentPath></AttachmentPath>
<AttachmentID></AttachmentID>
<AttachmentTitle></AttachmentTitle>
<AttachmentCaption></AttachmentCaption>
<AttachmentDescription></AttachmentDescription>
<AttachmentAltText></AttachmentAltText>
<Status>publish</Status>
<AuthorID>1</AuthorID>
<AuthorUsername>[AuthorUsername]</AuthorUsername>
<AuthorEmail>[AuthorEmail]</AuthorEmail>
<AuthorFirstName>[AuthorFirstName]</AuthorFirstName>
<AuthorLastName>[AuthorLastName]</AuthorLastName>
<Slug>[Slug]</Slug>
<Format></Format>
<Template>default</Template>
<Parent>0</Parent>
<ParentSlug>0</ParentSlug>
<Order>0</Order>
<CommentStatus>closed</CommentStatus>
<PingStatus>closed</PingStatus>
<PostModifiedDate>2023-11-14 18:16:40</PostModifiedDate>
<ImageGallery></ImageGallery>
<AdditionalInfo></AdditionalInfo>
<SupportGroup></SupportGroup>
<SidebarVideo></SidebarVideo>
<LogoSize></LogoSize>
<Logos></Logos>
<Sponsors></Sponsors>
<Members></Members>
</post>
</data>
我不太擅长API编码,所以我尝试使用ChatGPT大约一个小时。 WP All Import 开发人员告诉我,这是自定义功能,他们不支持 WPBakery,因此他们建议使用 ChatGPT。我无法找到有效的解决方案。 这是我尝试的最后一段代码:
add_action('pmxi_gallery_image', 'update_image_ids_after_import', 10, 2);
function update_image_ids_after_import($image_url, $post_id) {
// Fetch the XML data
$xml_data = PMXI_Plugin::getInstance()->session->xml_data;
// Get the content from the XML data
$content = $xml_data['post']['Content'];
// Extract image URLs from the content using regex
preg_match_all('/vc_single_image image="(\d+)"/', $content, $matches);
// Check if there are matches
if (!empty($matches[1])) {
// Loop through each matched image ID
foreach ($matches[1] as $key => $old_image_id) {
// Get the newly imported image ID
$new_image_id = attachment_url_to_postid($image_url);
// Replace the old image ID with the new one
$content = str_replace('vc_single_image image="' . $old_image_id . '"', 'vc_single_image image="' . $new_image_id . '"', $content);
}
// Update the Content node with the modified content
$xml_data['post']['Content'] = $content;
// Update the session with the modified XML data
PMXI_Plugin::getInstance()->session->xml_data = $xml_data;
}
}
@Shayne,您的代码的第一个问题是它使用单引号(')而不是双引号(“),据说使用的是:
注意使用双引号而不是单引号。从 WP All Import 中执行 PHP 函数时必须使用双引号。您不能使用单引号。
我要求 Claude.ai 改进 ChatGPT 中的代码片段,它附带了以下内容,我设法开始工作:
function update_wpbakery_image_ids($post_id, $xml_node, $is_update) {
// Get the post content
$content = get_post_field("post_content", $post_id);
// Skip if no content
if (empty($content)) {
return;
}
// Array of WP Bakery shortcodes that contain image IDs
$shortcode_patterns = array(
"vc_single_image" => "/\[vc_single_image[^\]]*image=\"(\d+)\"/",
"vc_gallery" => "/\[vc_gallery[^\]]*images=\"([^\"]+)\"/"
);
$modified = false;
foreach ($shortcode_patterns as $shortcode => $pattern) {
preg_match_all($pattern, $content, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $old_ids) {
// Handle both single IDs and comma-separated lists
$old_id_array = explode(",", $old_ids);
$new_id_array = array();
foreach ($old_id_array as $old_id) {
$old_id = trim($old_id);
// Get the attachment URL for the old ID
$old_attachment_url = wp_get_attachment_url($old_id);
if ($old_attachment_url) {
// Try to find the new attachment ID based on the same filename
$filename = basename($old_attachment_url);
$new_attachment = get_posts(array(
"post_type" => "attachment",
"post_status" => "inherit",
"fields" => "ids",
"meta_query" => array(
array(
"value" => $filename,
"compare" => "LIKE",
"key" => "_wp_attached_file"
)
)
));
if (!empty($new_attachment)) {
$new_id = $new_attachment[0];
$new_id_array[] = $new_id;
// Replace in content based on shortcode type
if ($shortcode === "vc_single_image") {
$content = preg_replace(
"/(\[vc_single_image[^\]]*image=\")" . $old_id . "\"/",
"$1" . $new_id . "\"",
$content
);
} elseif ($shortcode === "vc_gallery") {
$old_ids_pattern = preg_quote($old_ids, "/");
$new_ids = implode(",", $new_id_array);
$content = preg_replace(
"/(\[vc_gallery[^\]]*images=\")" . $old_ids_pattern . "\"/",
"$1" . $new_ids . "\"",
$content
);
}
$modified = true;
}
}
}
}
}
}
// Update post content if modifications were made
if ($modified) {
wp_update_post(array(
"ID" => $post_id,
"post_content" => $content
));
// Clear any caches
clean_post_cache($post_id);
}
}
我将此函数添加到函数编辑器中,该编辑器可在 WP All Import Pro 流程中使用。