Here is how you can apply conditionally a CSS class to link elements which contain images embedded into posts. Just add the code bellow into your wordpress functions.php file then check your image links, they should have post-image-link CSS class.
/**
* Attach a custom CSS class to linked images' parent anchors <a>..</a>
* Works for existing content too
*/
function linked_images_custom_class($content) {
$classes = 'post-image-link'; // space separate classes
//if there is already a class attribute assigned to the anchor tag
if ( preg_match('/<a.*? class=".*?"><img/', $content) ) {
$content = preg_replace('/(<a.*? class=".*?)(".*?><img)/', '$1 ' . $classes . '$2', $content);
} else {
// If there is not an existing class attribute, create a class attribute and assign it
$content = preg_replace('/(<a.*?)><img/', '$1 class="' . $classes . '" ><img', $content);
}
return $content;
}
add_filter('the_content','linked_images_custom_class');
Thanks for your time!
Best regards,
Lucas