Give img a class automatically

I have a theme, where the links/menus are styled with borders&padding which look odd on pictures, therefore I tried to add the following to my functinos.php
---
function add_imgspecial_class_to_image_links($html, $attachment_id, $attachment) {
$linkptrn = "/<a[^>]*>/";
$found = preg_match($linkptrn, $html, $a_elem);

// If no link, do nothing
if($found <= 0) return $html;

$a_elem = $a_elem[0];

// Check to see if the link is to an uploaded image
$is_attachment_link = strstr($a_elem, "wp-content/uploads/");

// If link is to external resource, do nothing
if($is_attachment_link === FALSE) return $html;

if(strstr($a_elem, "class=\"") !== FALSE){ // If link already has class defined inject it to attribute
$a_elem_new = str_replace("class=\"", "class=\"imgspecial ", $a_elem);
$html = str_replace($a_elem, $a_elem_new, $html);
}else{ // If no class defined, just add class attribute
$html = str_replace("<a ", "<a class=\"imgspecial\" ", $html);
}

return $html;
}

add_filter('image_send_to_editor', 'add_imgspecial_class_to_image_links', 10, 3);

---
Works fine in a single WP setup, but on WPMU, not so much.

I tried to change /wp-content/uploads to /files/, didn't do the trick

  • Jeffri
    • Front End Developer

    Hello bloggersdelight,

    To make the code work with multisite, it would be better to check using the wp_upload_dir() rather than 'wp-content/uploads/' string. Please try the following code that I have modified from yours:

    function add_imgspecial_class_to_image_links($html, $attachment_id, $attachment) {
    $linkptrn = "/<a[^>]*>/";
    $found = preg_match($linkptrn, $html, $a_elem);
    
    // If no link, do nothing
    if($found <= 0) return $html;
    
    $a_elem = $a_elem[0];
    
    // Check to see if the link is to an uploaded image
    $upload_path = wp_upload_dir();
    $is_attachment_link = strstr($a_elem, $upload_path['baseurl']);
    
    // If link is to external resource, do nothing
    if($is_attachment_link === FALSE) return $html;
    
    if(strstr($a_elem, "class=\"") !== FALSE){ // If link already has class defined inject it to attribute
    $a_elem_new = str_replace("class=\"", "class=\"imgspecial ", $a_elem);
    $html = str_replace($a_elem, $a_elem_new, $html);
    }else{ // If no class defined, just add class attribute
    $html = str_replace("<a ", "<a class=\"imgspecial\" ", $html);
    }
    
    return $html;
    }
    
    add_filter('image_send_to_editor', 'add_imgspecial_class_to_image_links', 10, 3);

    Hope that works. :slight_smile:

    Thank you.

    Jeffri

  • Jeffri
    • Front End Developer

    Hi there,

    The wp_upload_dir() function will automatically get the upload directory, so it should work out of the box with Wordpress and multisite setup. So a single Wordpress will get "wp-content/uploads/", but multisite will get "files/".

    Hope that help.

    Thank you.

    Jeffri

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.