You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP All Import导入后自动更新WPBakery Single Image图片ID的问题

问题:WP All Import迁移内容后WPBakery单图元素图片ID未更新

使用WP All Export/Import将WordPress生产站点的特定页面/文章及自定义字段迁移到测试站点时,WPBakery Page Builder的Single Image元素出现异常:图片已成功上传到测试站点并生成新ID,但文章内容中vc_single_image标签里的图片ID仍保留XML中的旧值,未替换为测试站点的新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>

尝试过的未生效代码

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=&quot;(\d+)&quot;/', $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=&quot;' . $old_image_id . '&quot;', 'vc_single_image image=&quot;' . $new_image_id . '&quot;', $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;
    }
}

解决方案

原代码存在两个核心问题:

  1. 钩子pmxi_gallery_image仅处理图库图片,不针对文章内容中的WPBakery短代码;
  2. 通过attachment_url_to_postid获取新ID不可靠,无法建立旧ID与新ID的准确映射。

正确实现代码如下:

add_action('pmxi_saved_post', 'replace_wpbakery_image_ids', 10, 3);

function replace_wpbakery_image_ids($post_id, $xml_data, $import_options) {
    // 获取目标文章内容
    $content = get_post_field('post_content', $post_id);
    if (empty($content)) return;

    // 提取所有vc_single_image短代码中的旧图片ID
    preg_match_all('/\[vc_single_image.*?image=&quot;(\d+)&quot;.*?\]/', $content, $matches);
    if (empty($matches[1])) return;

    // 去重旧ID,避免重复替换
    $old_image_ids = array_unique($matches[1]);
    $replacements = [];

    foreach ($old_image_ids as $old_id) {
        // 调用WP All Import内置方法,获取旧ID对应的新附件ID
        $new_id = PMXI_Plugin::getInstance()->mediaLibrary->get_attachment_id($old_id, $import_options['id']);
        
        if ($new_id && $new_id != $old_id) {
            $replacements['image=&quot;' . $old_id . '&quot;'] = 'image=&quot;' . $new_id . '&quot;';
        }
    }

    // 批量替换并更新文章内容
    if (!empty($replacements)) {
        $updated_content = strtr($content, $replacements);
        wp_update_post([
            'ID' => $post_id,
            'post_content' => $updated_content
        ]);
    }
}

使用说明

  1. 将代码添加到主题functions.php文件,或通过代码片段插件导入;
  2. 重新执行WP All Import导入任务,确保勾选「下载并导入图片/附件」选项(该选项是建立新旧ID映射的前提);
  3. 导入完成后,文章内的vc_single_image短代码图片ID会自动替换为测试站点的新ID。

内容的提问来源于stack exchange,提问作者Shayne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:54:50