0

I'm using filter like this to add rel attribute in wp_get_attachment_link()

add_filter( 'wp_get_attachment_link', 'sant_prettyadd'); 
function sant_prettyadd ($content) {
    $content = preg_replace("/<a/","<a rel=\"prettyPhoto[slides]\"",$content,1);
    return $content;
}

The above filter working fine except it adds the attribute even if the href points to attachment page.

I mean I want to add rel attribute only if the link points to direct image and not attachment page.

Can someone help me with this?

1 Answer 1

0
add_filter( 'wp_get_attachment_link', 'sant_prettyadd', 10, 6);

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

Update:

function sant_prettyadd checks permalink argument.

if the permalink = true then it returns the content as it is.

if the permalink = false then it skips to the next line and replace the <a with <a rel="prettyPhoto[slides]" and then returns the content.

2
  • For the benefit of future generations, explain what this code does ツ ◎ܫ◎
    – brasofilo
    Commented Feb 15, 2013 at 8:30
  • @brasofilo Updated my answer Commented Feb 15, 2013 at 9:44

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.