A Better Way to Remove Images from a WordPress Post
by Bandicoot Marketing on | Posted in Tutorials | 10 comments
I had originally written about Removing Images from a WordPress Post quite a while ago, and while that snippet still works, I recently discovered another way to do it that might help some people. This way hooks into the content filter.
<?php
function remove_images( $content ) {
$postOutput = preg_replace('/<img[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_images', 100 );
?>
If you include this in your functions.php it will remove every image from the content everywhere the_content() function has been used. That isn’t probably what you want, but what you can do is include this function in a specific page template, such as your index.php file, and then at the end of that file include the following to remove the filter:
<?php remove_filter( 'the_content', 'remove_images', 100 ); ?>

10 comments for “A Better Way to Remove Images from a WordPress Post”