WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 0 down vote accepted
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;


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.

share|improve this answer
For the benefit of future generations, explain what this code does ツ ◎ܫ◎ – brasofilo Feb 15 '13 at 8:30
@brasofilo Updated my answer – Giri Feb 15 '13 at 9:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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