After importing a blog from Blogger to self-hosted WordPress recently I discovered that all the inline images remain hosted on Blogger.
There were various hacks for moving them to my local server and updating the posts and suchlike – but in my case all the posts had a single image, and the importer had managed to set that image as the featured image. So all I really wanted to do was to strip the image and leave my theme to display the featured image.
I’ve got a scripts folder in my project root for just such occasions, here’s the script I used to remove all images from all WordPress posts:
<?php
$wordpressBasePath = __DIR__.'/../public/wp-load.php';
include_once $wordpressBasePath;
$query = new WP_Query([
'post_type' => 'post',
'nopaging' => true,
]);
// I figured I'd also use this opportunity to make sure the post content adheres to the new blog restrictions too
// plus kses is a handy way to strip the image tags.
$allowed_tags = wp_kses_allowed_html( 'post' );
unset($allowed_tags['img']);
while ($query->have_posts()) {
$query->the_post();
// Everyone loves an update, especially big websites
echo "\r".($query->current_post + 1).' of '.$query->found_posts.'. '.round((($query->current_post + 1) / $query->found_posts) * 100).'%';
$newPost = [
'ID' => get_the_ID(),
'post_content' => wp_kses(get_the_content(), $allowed_tags),
];
$error = wp_update_post($newPost, true);
if (is_wp_error($error)) {
print_r($errors);
}
}
echo "\n";
I’d like to also make this remove any empty anchor tags which get left behind (most of the images were wrapped in one) but without removing other anchors. If you have a reliable method for doing this, let me know in the comments!