Remove first image from WordPress posts

How to remove first image from WordPress posts

In this article you are going to find the step-by-step process to remove the first image from WordPress posts.

Each website is different, to find the optimal spot to place an image or an ad to capture the visitor’s attention is a challenge, and this also involves a lot of testing. But… what happens when you have published all of your posts in a certain way and, while testing, you discovered that the first image is not doing its job? Would you go post-by-post and remove it manually? Yeah, right! I don’t think that you’re going to spend a week or more of work to modify as many of them you may have. So, what is the solution? -The solution is to create a new function in your WordPress theme to take care of the problem, which it will give you the ability to revert back if you change your mind, and all your posts will have the first image back.

Before you start by modifying files and adding new code, make sure to backup your site:

If this is your case, here is one solution to remove first image.

First, connect to your WordPress site and open the functions.php file, look for an empty spot and paste the following code.

function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
} return $content;
}
add_filter('the_content', 'remove_first_image');

Save the file, clear the website’s cache if any, and now you should see the single post without the first image. That simple!