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):
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);
});
I tested it locally and it is working, let me know if that helps.
Related Posts:
- remove links from images using functions.php
- Missing feature image link function
- Add image size if page template
- Set JPEG compression for specific custom image sizes
- Issue with wp_get_attachment_image() and SVG file type
- How to change the markup WordPress inserts for post images
- Programmatically Set First Image as Featured
- Removing default image size list in Media Box
- WP 4.4. responsive loads normal image after loading the responsive image
- How to create thumbnails for PDF uploads?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- Set image size on media and text block with a function
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Create thumbnail on PDF upload with Gravity Forms
- Gallery images titles – get from post
- Remove images from get_the_excerpt
- Use Exact Image Size using add_image_size
- How to add a rel attribute to images that contains their categories?
- Why are image thumbnails cropped proportionally (not per dimensions)?
- Insert width & height attributes to all images displayed on each page
- Best way to programatically add “rel” attributes to page and post images
- Overwrite Parent Theme add_image_size in Child Theme
- Images uploaded using media uploader are appearing upside down
- How to add css class to image attached in all the posts?
- Add custom image size not working
- Count total number of images in post and echo results as number
- How can you limit srcset on a single type of page?
- how to add an image to the footer?
- wp_get_attachment_image function size argument not working if I also add a custom class
- Automatically wrap multiple images in div
- Understanding and altering the structure of posted images
- Overwriting Parent Theme’s Image Sizes
- Image rotation fails to regenerate custom sized thumbnail
- Publish/Update post is changing image links from file url to post url
- Featured Image Thumbnail Creation
- Twentyten Child Theme: header images display messed up in admin panel
- Display images that are not in the content
- How can I modify all existing tags while keeping the urls themselves?
- Add dynamic links in WordPress custom theme
- show first 3 thumbnails of posts in different sizes [closed]
- Getting custom image size URL in functions
- Set spesific size of featured images
- media resize/crop possibilities?
- Can’t filter wp_get_attachment_link
- Get arguments from URL
- Control the srcset much more (not all sizes in it each time)
- Way to store featured image width and height in two separate variables?
- srcset and custom field get_post_meta and wp_get_attachment_url
- Limit dimensions of “Full” size images
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- javascript and css links not working on sever
- What are the meta fields for an attachment?
- Using Static HTML instead of the `home_url()` WP function
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- How to properly set custom logo size?
- Add attribute only to first image of every post via functions.php
- Need To Change WordPress Meta Tags… Includes wordpress shortcodes
- Need to convert image url to a Base_64 data url with wordpress function..
- How to add a new image size and apply it to posts only?
- Function to insert missing image size attributes into img tags
- Image Crop Not Working
- Use existing image sizes for WooCommerce
- How to convert raw url to hyperlink?
- Retrieve First Image Function
- Custom Image Size in Insert Media
- Using add_image_size in functions.php to get original uploaded file
- get_image_tag() html output : empty src attribute
- Need To Resize Images Exactly Without Losing Image Details
- Custom image sizes showing in Classic Editor only when upladed directly to post
- how can I change all wordpress media file url to custom cdn url?
- Auto delete content in specific folder inside media library
- How to change images url in function.php?
- How to update an image attachment’s alt text from a custom field when saving a post?
- Best practice to call a function from a button-link?
- Class parameter in get_avatar args doesn’t get added to output
- large image size of 1440×1440 not pulling 2x of 2880×2880 image
- blank page with wp_get_attachment
- Images with overlay
- How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?
- Changing author links on homepage to buddypress profiles – without affecting ability to link to author archive throughout the site
- Featured image on archive page based on post type
- Printing medium sized image
- restrict uploaded image size and fixed image display size
- Why am I losing image quality on my site?
- Replace image attributes for lazyload plugin on image elements with specific class
- Restricted images only for logged user. (hidden url)
- Zip all (original) images from media gallery
- No compression occurs on my jpegs after adding jpeg_quality hook to my functions.php file
- Update add_image_size
- Function the_content
- Open image size links in a page instead of direct image link
- How to Add A Rel Filter to All Links?
- Add gallery id to rel attribe of wp_get_attachment_link
- Dynamically adding Captions to images
- Full size image option removing problem
- Function to get thumbnail img source
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- Image width issue in IE [closed]
- image_size with respect to aspect ratios
- How to crop image in WordPress with aspect ratio for any size of image