Skip to main content
Please format your code to a reasonable standard and stop using shamless self-plugging links.
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

youYou just need to add this to your theme's functions.phpfunctions.php file:

/**
* Attach a class to linked images' parent anchors
* e.g. a img => a.img img
*/
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size,     $alt = '' ) {
    $classes = 'img'; // separated by spaces, e.g. 'img image-link'

    // check if there are already classes assigned to the anchor
    if ( preg_match('/<a.*? class=".*?">/', $html) ) {
    $html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
    } else {
     $html = preg_replace('/(<a.*?)>/', '$1 class="' . $classes . '" >', $html);
    }
    return $html;
} 

add_filter('image_send_to_editor','give_linked_images_class',10,8);

you just need to add this to your theme's functions.php file:

/**
* Attach a class to linked images' parent anchors
* e.g. a img => a.img img
*/
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size,     $alt = '' ){
$classes = 'img'; // separated by spaces, e.g. 'img image-link'

// check if there are already classes assigned to the anchor
if ( preg_match('/<a.*? class=".*?">/', $html) ) {
$html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
} else {
 $html = preg_replace('/(<a.*?)>/', '$1 class="' . $classes . '" >', $html);
}
return $html;
}
add_filter('image_send_to_editor','give_linked_images_class',10,8);

You just need to add this to your theme's functions.php file:

/**
* Attach a class to linked images' parent anchors
* e.g. a img => a.img img
*/
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
    $classes = 'img'; // separated by spaces, e.g. 'img image-link'

    // check if there are already classes assigned to the anchor
    if ( preg_match('/<a.*? class=".*?">/', $html) ) {
    $html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
    } else {
     $html = preg_replace('/(<a.*?)>/', '$1 class="' . $classes . '" >', $html);
    }
    return $html;
} 

add_filter('image_send_to_editor','give_linked_images_class',10,8);
Source Link
jim.duck
  • 159
  • 2
  • 5

you just need to add this to your theme's functions.php file:

/**
* Attach a class to linked images' parent anchors
* e.g. a img => a.img img
*/
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size,     $alt = '' ){
$classes = 'img'; // separated by spaces, e.g. 'img image-link'

// check if there are already classes assigned to the anchor
if ( preg_match('/<a.*? class=".*?">/', $html) ) {
$html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
} else {
 $html = preg_replace('/(<a.*?)>/', '$1 class="' . $classes . '" >', $html);
}
return $html;
}
add_filter('image_send_to_editor','give_linked_images_class',10,8);