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.

  • 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, 2011 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, 2011 at 9:30
  • 1
    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, 2011 at 11:09

1 Answer 1


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);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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