1

So I need to add a class to links around the images for lightbox purposes. But only to images from the upload directory, not to external ones. I've found this solution by @TommiForsström here:

function add_colorbox_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=\"colorbox ", $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=\"colorbox\" ", $html);
}

return $html;
}

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

Looks like it's exactly what I need, except it doesn't work. I've tried it on a clean WP thirty thirteen install as well. Any thoughts? Thanks!

1 Answer 1

2

Well, I'll answer my dumb question myself. As it explained by nice fellow, the image_send_to_editor filter runs inside the get_image_send_to_editor function which is responsible for sending the link HTML that surrounds images sent to the editor. This filter runs when you insert image using Media Uploader to the editor. It doesn’t applied to your existing images in your existing posts/pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.