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 use a lightbox to display images from posts in a website that is updated by differents authors.

Or course, the lightbox only works if the author makes sure that he uses link to the image directly while uploading the file.

1/3 of the time, the author forgets and the lightbox shows the html of the attachment.php page.

Is there possible to have a function, placed into attachments.php, that would return the image instead of the html page ? It would be simple to find the image itself, but I guess this would requires to change headers somewhere so the webserver returns the 'image/jpg' header.

I would manage the details but I would need some help to start with.

Thank you.

share|improve this question
Why not change the default link behaviour, so there's less need for the authors to remember? View yoursite.com/options.php and change image_default_link_type to file, if that's suitable for you. – t31os Dec 6 '11 at 19:52
Thanks for the tip. The option was already set to file, so I guess authors are clicking on the wrong button. I did read somewhere of a function that replace the content of the html page by a image, maybe with something like header('Content-Type: image/jpeg'); readfile('path/to/myimage.jpg'); – Simon Dec 7 '11 at 9:30
The lightbox script could also be adjusted(in theory) to only add the lightbox classes to images when they're set to file(assuming the lightbox script also works based on class and doesn't just attach to all post images). – t31os Dec 7 '11 at 11:09
up vote 1 down vote accepted

Answer is this (thanks to Wordpress Vampire here : Change Attachment Post URLs to File URLs) :

Create an attachment template file within the theme. Since we are only interested in images, the file should be image.php


if ( have_posts() ) { 
    $image_url = wp_get_attachment_url();

header('Location: '.$image_url);

share|improve this answer

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.