Created
February 26, 2017 03:03
-
-
Save dtbaker/46d98da24b8430b761e5c1f380c76b91 to your computer and use it in GitHub Desktop.
Hacky way to import pictures after a WordPress site migration that didn't go well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will search through all blog content and find/replace <a href> and <img src> links | |
// It will find images on the old multi-site host, e.g. http://dtbaker.net/wp-content/uploads/sites/* and import those images into the new blog, then update the links/src in the post content. | |
add_action('init', function(){ | |
if(isset($_REQUEST['do_import'])){ | |
ini_set('display_errors',true); | |
ini_set('error_reporting',E_ALL); | |
require_once ('wp-admin/includes/admin.php'); | |
$posts = get_posts(array( | |
'posts_per_page' => '-1' | |
)); | |
foreach($posts as $post){ | |
$post = (array)$post; | |
$content = $post['post_content']; | |
$content = str_replace('https://new.dtbaker.net/wp-content', 'https://dtbaker.net/wp-content', $content); | |
$content = str_replace('http://new.dtbaker.net/wp-content', 'https://dtbaker.net/wp-content', $content); | |
if( preg_match_all('#<a.*href="(https?://dtbaker\.net/wp-content/uploads/sites/[^"]+)"[^>]*>#imsU', $content, $matches)){ | |
print_r($matches); | |
foreach($matches[1] as $key => $url){ | |
$url = str_replace('dtbaker.net','old.dtbaker.net',$url); | |
echo "Importing $url "; | |
$imported_url = media_sideload_image($url, $post['ID'], basename($url), 'src'); | |
if($imported_url){ | |
echo " ... as $imported_url <br>\n"; | |
$new_image_tag = str_replace( $matches[1][$key], $imported_url, $matches[0][$key] ); | |
echo "New tag is: $new_image_tag \n"; | |
$content = str_replace($matches[0][$key], $new_image_tag, $content); | |
}else{ | |
echo "FAILED TO IMPORT"; | |
} | |
} | |
} | |
if( preg_match_all('#<img.*src="(https?://themes\.dtbaker\.net/wp-content/uploads/sites/[^"]+)"[^>]*>#imsU', $content, $matches)){ | |
print_r($matches); | |
foreach($matches[1] as $key => $url){ | |
$url = str_replace('themes.dtbaker','old.dtbaker',$url); | |
echo "Importing $url "; | |
$imported_url = media_sideload_image($url, $post['ID'], basename($url), 'src'); | |
if($imported_url){ | |
echo " ... as $imported_url <br>\n"; | |
$new_image_tag = str_replace( $matches[1][$key], $imported_url, $matches[0][$key] ); | |
echo "New tag is: $new_image_tag \n"; | |
$content = str_replace($matches[0][$key], $new_image_tag, $content); | |
}else{ | |
echo "FAILED TO IMPORT"; | |
} | |
} | |
} | |
if( preg_match_all('#<img.*src="(https?://dtbaker\.net/wp-content/uploads/sites/[^"]+)"[^>]*>#imsU', $content, $matches)){ | |
print_r($matches); | |
foreach($matches[1] as $key => $url){ | |
$url = str_replace('dtbaker.net','old.dtbaker.net',$url); | |
echo "Importing $url "; | |
$imported_url = media_sideload_image($url, $post['ID'], basename($url), 'src'); | |
if($imported_url){ | |
echo " ... as $imported_url <br>\n"; | |
$new_image_tag = str_replace( $matches[1][$key], $imported_url, $matches[0][$key] ); | |
echo "New tag is: $new_image_tag \n"; | |
$content = str_replace($matches[0][$key], $new_image_tag, $content); | |
}else{ | |
echo "FAILED TO IMPORT"; | |
} | |
} | |
} | |
if($content && $content != $post['post_content']){ | |
echo $post['ID']. " "; | |
$my_post = array( | |
'ID' => $post['ID'], | |
'post_content' => $content, | |
); | |
wp_update_post($my_post); | |
} | |
} | |
echo 'done'; | |
exit; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment