So I have code in place to remove the first image of all posts (this is because I put an image at the top of every post before featured images were introduced, and once featured images were created, I just put code in place to remove the first image so I wouldn't have to go back and do it manually). I'm trying to get it work for AMP pages, but haven't been able to figure it out yet.
This is the function for the normal pages.
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');
And since that doesn't remove the first image on AMP pages, I tried something like this which has some correct elements in it but isn't working.
add_action( 'pre_amp_render_post', function () {
add_filter( 'the_content', function( $content ){
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
return $content;
}, 3 );
});
Any thoughts on how to get this to work for AMP content?