• Hi

    Im getting duplicated images in old posts: appears the featured image and below the first image of the post (this is the same image of featured image).

    I want to delete the first image of the post and maintain the featured image in old posts. But… i have some recent posts (+5000) with featured image and the first image deleted with plugin… so, what options i have?

    Because if i disable featured images, thousands posts are without any image (because i have the first image removed)

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    Try this snippet in functions.php at the end of the file.

    function xx_remove_first_image( $content ) {
    
    	if ( preg_match( '/(<img [^>]*>)/', $content, $matches ) ) {
    		$content = str_replace( $matches[1], '', $content );
    	}
    
    	return $content;
    }
    add_filter( 'the_content', 'xx_remove_first_image', 20 );

    That should remove the first image within the post content. But will not remove it from the content itself, simply the image will not be shown up.

    If you want to remove completely the image from the post content, you need to hook into the wp_insert_post_data filter. In this case make a database backup first.

    Thread Starter xopox

    (@xopox)

    Thank you so much Guido!!! it works

    Another question, do you know the snippet to remove completely from the database with the wp_insert_post_data function? Thanks again man!

    Thread Starter xopox

    (@xopox)

    Hi again Guido,

    I have noticed this PHP snippet has removed in frontpage the image of the first post.

    How i can avoid this? Thanks

    Of course, try the code below. Consider in this case the image will be removed only in single post post-type.

    function xx_remove_first_image( $content ) {
    
    	if ( is_singular( 'post' ) && preg_match( '/(<img [^>]*>)/', $content, $matches ) ) {
    		$content = str_replace( $matches[1], '', $content );
    	}
    
    	return $content;
    }
    add_filter( 'the_content', 'xx_remove_first_image', 20 );

    Regarding the way to remove them from the post during update or insert you should consider that every first image will be removed even if it is not a duplicated of the featured image.

    Thread Starter xopox

    (@xopox)

    Hi Guido,

    Now it works, but now the problem is the next:

    In the newer posts i have without first images, this snippet try to remove the 2º image (because the first image is uploaded directly to featured image) and only shows the first image and 3º image.

    At this point, i think the only option is delete by a SQL snippet :S

    Uhm, you’re right.

    I made some changes and I think the only way to remove the duplicated image is to perform a check with the ID of the featured image and the image within the content.

    Have a look at this new one.
    The function get the post thumbnail ID and compare it with the ID get from the wp-image- class value. Generally WordPress add that class with the ID of the image.

    If the post has not a featured image or the ID of the first image found is not the same of the post thumbnail ID nothing will be performed.

    If this code work correctly, we can try to remove the image directly within this function.

    Sorry for the issue, I thought that you had all posts with the duplicated image.

    Let me know.

     
    function xx_remove_first_image( $content ) {
    
    	if ( ! is_singular( 'post' ) || ! is_main_query() ) {
    		return $content;
    	}
    
    	// Get the featured image.
    	$_fthumb = absint( get_post_thumbnail_id() );
    
    	// Do nothing if the post hasn't a featured image.
    	if ( ! $_fthumb ) {
    		return $content;
    	}
    
    	// Try to retrieve the ID of the attachment.
    	if ( preg_match( '/<img class=\".*wp\-image\-([\d]{1,}).*\" [^>]*>/', $content, $matches ) && ! empty( $matches[1] ) ) {
    		$_id = absint( $matches[1] );
    
    		// And compare it with the featured image thumbnail.
    		if ( $_id === $_fthumb ) {
    			preg_match( '/(<img [^>]*>)/', $content, $matches );
    
    			if ( ! empty( $matches[1] ) ) {
    				$content = str_replace( $matches[1], '', $content );
    			}
    		}
    	}
    
    	return $content;
    
    }
    add_filter( 'the_content', 'xx_remove_first_image' );
    Thread Starter xopox

    (@xopox)

    Hi mate,

    Sorry for the delay in reply, i thought i have replied before.

    Thank you again for your help, i really appreciate it!

    I tried the last code you sent, and now works but only with random posts. There is a lot of posts still showing duplicated images, but in others posts they had duplicated images now only one.

    Do you know why happens? its possible because they post don’t have class ID in the image?

    Thanks

    Hi,

    Yes it is correct, if the image has no class wp-image-{ID} the image will not be removed because there is no way to compare it with the featured image.

    I have not compared the src attribute value of the images ’cause it is possible to use different image sizes within the post editor.

    I think a more accurate regexp should resolve this issue but I have no clue right now.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Duplicated images’ is closed to new replies.