I am trying to transfer parts of a Wordpress installation from a live site to my test site using WP All Export / Import. I don't need the entire site transferred, just certain pages/post with certain custom fields, for testing. The "Single Image" elements from within WPBakery Page Builder are giving me trouble after an import. The image IDs are keeping the values from the xml file instead of updating the image IDs to the new ID values after the image is uploaded to the test site.
Here is an image showing what's going on. Image Ids 14821, 14819,14820, etc are not being updated to the new values after the import. Image IDs Not Updating
Each image correctly gets uploaded to the site (with new image IDs), but the image IDs in the post do not get changed.
Here is a sample of the xml data. Note: Values surrounded by [] denotes information has been changed for security purposes.
<?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>
I'm not very good at API coding, so I tried using ChatGPT for about an hour. The WP All Import developers informed me that this is custom functionality and that they don't support WPBakery, so they suggested ChatGPT. I wasn't able to get a solution that worked. Here is the last bit of code that I tried:
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;
}
}