WordPress - Remove the featured image from the content in feeds
<?php | |
/** | |
* Removes the "featured image" from the content | |
* Google news stand/Facebook Instant Articles do not like it | |
* Even different sizes of the Featured Image throw warnings | |
*/ | |
function remove_featured_image_from_content( $content ) { | |
// Get the post ID | |
$post_ID = get_the_ID(); | |
if ( has_post_thumbnail( $post_ID ) ) { | |
// Get the featured image's data | |
$image_ID = get_post_thumbnail_id( $post_ID ); | |
$image_info = wp_get_attachment_metadata( $image_ID ); | |
$upload_dir = wp_upload_dir(); | |
$upload_folder = $upload_dir['baseurl']; | |
$image_date = get_the_date( 'Y/m', $image_ID ); | |
// Check all the images of the post against the featured image and it's generated sizes | |
if ( isset( $image_info['sizes'] ) ) { | |
foreach ( $image_info['sizes'] as $image_size) { | |
$file_name = $upload_folder .'/'. $image_date .'/'. $image_size['file']; | |
$file_name = preg_quote($file_name, '/'); | |
// check if it's wrapped in a figure tag, then check for the image tag alone | |
$content = preg_replace( '/<figure.*?><img.*?src="('. $file_name .')".*?\>.*?<\/figure>/i', '', $content ); | |
$content = preg_replace( '/<img.*?src="('. $file_name .')".*?\>/i', '', $content ); | |
// remove any empty anchor links that might have been left behind | |
$content = preg_replace( '/<a[^>]*><\/a>/', '', $content ); | |
} | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content_feed', 'remove_featured_image_from_content' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment