Skip to content

Instantly share code, notes, and snippets.

@trey8611
Last active April 3, 2024 03:18
Show Gist options
  • Save trey8611/1f4314ef2f656457f8948d395a5dae6b to your computer and use it in GitHub Desktop.
Save trey8611/1f4314ef2f656457f8948d395a5dae6b to your computer and use it in GitHub Desktop.
How to migrate shortcode image IDs | Visual Composer / WPBakery / ETC

Migrate shortcode image IDs with WP All Export and WP All Import

Convert the IDs to URLs during the export

Use a custom PHP function ( https://www.wpallimport.com/documentation/export/pass-data-through-php-functions/ ) on the content field to convert the image IDs to URLs. Example shortcode:

[vc_single_image image="177" img_size="full" add_caption="yes" alignment="center" style="vc_box_shadow_border"]

Example function:

function my_convert_images( $content ) {
	if ( empty( $content ) ) return;
	
	preg_match_all( '/image="(.*?)"/', $content, $matches );
	if ( empty( $matches ) ) return $content;
	
	foreach ( $matches[0] as $match ) {
		$img_id = filter_var( $match, FILTER_SANITIZE_NUMBER_INT );
		$img = wp_get_attachment_url( $img_id );
		$content = str_replace( $match, 'image="' . $img . '"', $content );
	}
	return $content;
}

Example usage: https://d.pr/i/2smxLa.

Import the URLs and new IDs in the content

Next, we'll use WP All Import's API ( http://www.wpallimport.com/documentation/developers/action-reference/ ) to import the image URLs and new IDs in the content.

Example code:

function my_update_content( $id, $xml, $u ) {
	global $wpdb;
	$post = get_post( $id );
	$content = $post->post_content;
	preg_match_all( '/image="(.*?)"/', $content, $matches );
	
	if ( empty( $matches ) ) return;
	
	foreach ( $matches[0] as $match ) {
		$new_image_id = PMXI_API::upload_image( $id, $match, 'yes', false, true );		
		if ( ! empty( $new_image_id ) ) {
			$content = str_replace( $match, 'image="' . $new_image_id . '"', $content );
		}
	}
	
	$args = array( 'ID' => $id, 'post_content' => $content );
	wp_update_post( $args );
}

add_action( 'pmxi_saved_post', 'my_update_content', 10, 3 );
@lukartel040978
Copy link

lukartel040978 commented Mar 29, 2022

Please help me a little, the other import function doesn't work. Pull the images into the gallery, do everything nicely, only in the end the URL does not change to the image ID.

function my_update_content ($ id, $ xml, $ u) {
global $ wpdb;
$ post = get_post ($ id);
$ content = $ post-> post_content;
preg_match_all ('/image="(.*?)"/', $ content, $ matches);

if (empty ($ matches)) return;

foreach ($ matches [0] as $ match) {
$ new_image_id = PMXI_API :: upload_image ($ id, $ match, 'yes', false, true);
if (! empty ($ new_image_id)) {
$ content = str_replace ($ match, "image =" '. $ new_image_id.' "', $ content);
}
}

$ args = array ('ID' => $ id, 'post_content' => $ content);
wp_update_post ($ args);
}

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

@trey8611
Copy link
Author

trey8611 commented Apr 5, 2022

@lukartel040978 this is only example code and it may need to be adjusted in different environments using different data. Please try using this alternative regex:

preg_match_all( '/(?<=image=")(.*?)(?=")/', $content, $matches );

And replace the str_replace in the loop with this:

$content = str_replace( $match, $new_image_id, $content );

If that doesn't fix it, or if you need further help with this custom code, you'll need to work with a developer or ask for help here: https://stackoverflow.com/.

@lukartel040978
Copy link

Thank you! Working!

@flytech01
Copy link

I have modded for working with gallery images to:

EXPORT:
function my_convert_images($content)
{
if (empty($content)) return;

// single images
preg_match_all('/image="(.*?)"/', $content, $matches);
if (!empty($matches)) {
    foreach ($matches[0] as $match) {
        $img_id = filter_var($match, FILTER_SANITIZE_NUMBER_INT);
        $img = wp_get_attachment_url($img_id);
        $content = str_replace($match, 'image="' . $img . '"', $content);
    }
}

// gallery images
preg_match_all('/images="(.*?)"/', $content, $matches);
if (!empty($matches)) {
    foreach ($matches[0] as $match) {
        $match_pieces = explode(",", $match);
        foreach ($match_pieces as $piece) {
            $img_id = filter_var($piece, FILTER_SANITIZE_NUMBER_INT);
            $imgs[] = wp_get_attachment_url($img_id);
        }
        $content = str_replace($match, 'images="' . implode(",", $imgs) . '"', $content);
    }
}

return $content;

}

IMPORT:
// function for visual composer image upload wpallimport function.php
function my_update_content($id, $xml, $u)
{
global $wpdb;
$post = get_post($id);
$content = $post->post_content;

// preg_match_all( '/image="(.*?)"/', $content, $matches );
preg_match_all('/(?<=image=")(.*?)(?=")/', $content, $matches);
if (!empty($matches)) {

    foreach ($matches[0] as $match) {
        $new_image_id = PMXI_API::upload_image($id, $match, 'yes', false, true);
        if (!empty($new_image_id)) {
            // content = str_replace( $match, 'image="' . $new_image_id . '"', $content );
            $content = str_replace($match, $new_image_id, $content);
        }
    }
}

preg_match_all('/(?<=images=")(.*?)(?=")/', $content, $matches);
if (!empty($matches)) {

    foreach ($matches[0] as $match) {

        $match_pieces = explode(",", $match);
        foreach ($match_pieces as $piece) {

            $new_image_id = PMXI_API::upload_image($id, $piece, 'yes', false, true);
            if (!empty($new_image_id)) {
                $content = str_replace($piece, $new_image_id, $content);
            }
        }
    }
}


$args = array('ID' => $id, 'post_content' => $content);

wp_update_post($args);

}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment