Skip to content

Instantly share code, notes, and snippets.

@benfavre
Created June 21, 2022 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfavre/591bbbeea8ee4c6230e368251c095cef to your computer and use it in GitHub Desktop.
Save benfavre/591bbbeea8ee4c6230e368251c095cef to your computer and use it in GitHub Desktop.
Remove featured image if it appears in "the_content" WordPress filter
```php<?php
add_filter( 'the_content', function ($content) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$featured_image = get_the_post_thumbnail_url($post->ID);
$doc = new DOMDocument();
$doc->loadHTML($content, );
$imgs = $doc->getElementsByTagName('img');
for($i = $imgs->length; --$i >= 0;){
$node = $imgs->item($i);
if (strpos($node->getAttribute('src'), $featured_image) !== false) {
$node->parentNode->removeChild($node);
}
}
return $doc->savehtml();
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment