I am not a professional when it comes to regular expressions but this should be able to catch all linked images which the href attribute links to an image file ((png|jpg|gif|jpeg|bmp) feel free to add/remove extensions):
1 2 3 4 5 6 7 8 9 10 11 12 | add_filter('the_content', function($c){ return preg_replace_callback("#<\s*?a\b[^>]*>(.*?)</a\b[^>]*>#s", function($m){ $tpl = $m[0]; $cnt = isset($m[1]) ? $m[1] : null; if ( preg_match('/<a(.*?)?href=[\'"]?.+\.(png|jpg|gif|jpeg|bmp)[\'"]?(.*?)?><\/a>/i', str_replace($cnt, '', $tpl)) ) { return $cnt; } else { return $tpl; } }, $c); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.