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've just started using the attachment.php template on my site. What I'd like to do is redirect all images in posts that used 'file url' to 'post url' and my new attachment.php template.

Is this possible?

Thanks in advance for any help.


share|improve this question
It might help to provide your code for the attachment.php. – Jeremy Jared Aug 8 '11 at 22:34
For clarity(as i think i follow the question), Everton is asking how he can update all posts that currently reference an attach in their content and point to the file url, and replace those URLs with the individual attachments URL. Something you'll likely only get done with custom SQL and regex...(there's no quick one liner for this).. – t31os Aug 9 '11 at 0:11

Link to the image file using wp_get_attachment_link( $id, $size, $permalink, $icon, $text ):

  • $id is the attachment ID, which, if using attachment.php, will simply be $post->ID
  • $size is the image size, e.g. full, large, medium, thumbnail
  • $permalink determines whether a link to the file or post is returned. Use false (which is the default) to link to the file
  • $icon refers to a mime-type icon. Leave false, which is default
  • $text displays a text link. Leave false, which is default
share|improve this answer
thanks for the answer. How and where do I add this code so that old posts linking to images via the file url, will 'redirect' to the post url for the image? – Everton Blair Aug 10 '11 at 9:16
It would go in the Loop, and replace the current code, which, I'm guessing is simply the_content(), in attachment.php – Chip Bennett Aug 10 '11 at 11:44

Take a look at the solution on the WordPress Support Forum: http://wordpress.org/support/topic/redirect-attachment-page-to-post-page-how-to

share|improve this answer
Thanks, but this goes the wrong way around - post url 2 file url, not file url 2 post url – Everton Blair Aug 10 '11 at 9:17

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.