0

I'm trying to modify all image attachment links to include a rel value thus:

add_filter( 'wp_get_attachment_link', 'my_prettyadd', 100, 6);

function my_prettyadd( $markup, $id, $size, $permalink, $icon, $text ) {
    $content = preg_replace("/<a/","<a rel=\"prettyPhoto[slides]\"",$markup,1);
    return $content;
}

This has worked fine for me for ages, but recent installation of WP have meant this fails in so far that the function is never called (I placed an exit; line in there and it has no effect. I know that my theme does not use wp_get_attachment_link and so I'm suspecting I'm barking up the wrong tree now.

I'm using Canvas by WooThemes - I don't know if they have done anything out of the ordinary with attachments in posts. Anything obviously wrong in what I"m doing?

1 Answer 1

0

Have you tried using the 'the_content' filter? Something like this should probably do the Job , note that i have not had the time to test this .

function my_prettyadd($content) {
    global $post;
    $pattern        = "/(<a(?![^>]*?rel=['\"]prettyPhoto.*)[^>]*?href=['\"][^'\"]+?\.(?:bmp|gif|jpg|jpeg|png)['\"][^\>]*)>/i";
    $replacement    = '$1 rel="prettyPhoto['.$post->ID.']">';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
 }
 add_filter("the_content","my_prettyadd");

simple regex that will add a rel to all images on the website

1
  • It had occurred to me to try that, but I'm really trying to find out why the original filter was failing. <chuckle> negative look ahead - simple regex - for some :) Commented Feb 12, 2014 at 16:46

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.