Created
June 21, 2022 21:25
-
-
Save benfavre/591bbbeea8ee4c6230e368251c095cef to your computer and use it in GitHub Desktop.
Remove featured image if it appears in "the_content" WordPress filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```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