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

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