WordPress: Disabling auto image links to attachment pages
As you will have noticed if you are used to using wordpress, it has a horrid inbuild script to automatically link images in an image gallery through to a new page just showing the image with a really ugly page name. The whole thing is rather pointless and lets hope they fix it sometime soon.
However I am chuffed to say with my fairly average php skills I found a fix which I have used to then link to my attachement images in a lightbox. Its not theme based which will mean it will get thorwn out with updates I guess – but I don’t know how else one would do it and I couldn’t find a solution that worked on the net.
So heres what I did:
1, Install a lightbox plugin or hardcode the lightbox script into your theme. I have used various ones – Lightbox plus being the latest. This will then automcially insert the lightbox script into your page allowing us to hardcode a rel statement into the link to our image.
2, Now in wp-includes find the media.php file and find the lines with:
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && ‘file’ == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
which is about line 852 in my current version of wordpressnow delete the $size I have bolded above and save
3, Find the post-template.php file and on line 1174 (my version of wordpress 3.2.1) it says:
return apply_filters( ‘wp_get_attachment_link’, “<a href=’$url’ title=’$post_title’>$link_text</a>”, $id, $size, $permalink, $icon, $text );
}now insert rel=’lightbox’ after the post_title section so it reads
return apply_filters( ‘wp_get_attachment_link’, “<a href=’$url’ title=’$post_title’ rel=’lightbox’>$link_text</a>”, $id, $size, $permalink, $icon, $text );
}
That will then make your image link to its file, not the attachment page. It will also prevent the cropping that occurs in gallery which is what I wanted.
If you have a further question about coding and stuff – dont ask me as my php skills at this level are weak at best – but I was so amazed I found a solution I thought I’d share it.
Read More