I have a client with a very image heavy blog. Every time they upload images in their posts they are using the Attachment Post URL for the link instead of the File URL. The problem with this is that there is an image plugin that automatically zooms images nicely if they are links to the file but won't work if it's a link to the attachment page.

Is there any way to force all links to attachment URLs to point to the File URL instead?

I tried redirecting my attachment.php but that won't change the link in the index.php or single.php. Thanks for your help. I can't go through and change every existing post or prevent the client from doing this in the future.

3 Answers 3


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


Template Hierarchy

add_filter('image_send_to_editor', 'image_send_to_editor_rewrite', 1, 8);

function image_send_to_editor_rewrite($html, $id, $caption, $title, $align, $url, $size, $alt = '') {

    $html = get_image_tag($id, $alt, $title, $align, $size);
    $rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';

        // get file url
    $src = wp_get_attachment_image_src($id, $size);

    if ( $url )
            // force href to file url $src[0]
    $html = '<a href="' . esc_attr($src[0]) . "\"$rel>$html</a>";

    return $html;   

EDIT: This won't help for existing posts either, sorry. For existing posts one could hook into "the_content" use some regex to find ?attachment_id=X , get the the file url and replace the 'href', but this would delay page loading as well and if you're using pretty permalinks with %postname% it wouldn't make things easier.

But for future posts, above works. Quick & dirty, does the job, but linking to an attachment page won't be possible anymore. May also result in a bad user experience if your client clicks the "attachment post url" button on purpose, but image always links to the file.

Maybe it'd better to hook into the thickbox and remove/hide the 'attachment post url" button with javascript or simply via css

  • Where does $rel come from?
    – fuxia
    Jan 29, 2012 at 14:16
  • it's from the original function which calls apply_filters('image_send_to_editor'...). see wp-admin/includes/media.php around line 112, which gets called by image_media_send_to_editor on line 884, which is a callback for the filter 'media_send_to_editor'. As said, quick & dirty... Jan 29, 2012 at 16:11

This is the really easy way to link inserted pics to the original file instead of the attachment page. http://wordpress.org/support/topic/make-image-attachments-default-link-to-original-image?replies=4

There is a hidden options page in WordPress under yoursite.com/wp-admin/options.php On that endless list of undocumented options you can set a value for "image_default_link_type". You may want to set it to "file" (without the quotes)

Or, this will set the link to the 'large' image, which is probly the best choice. So when they go to insert, it will auto majically be set to large.

Neither work retroactively, sorry.

add_filter('attachment_fields_to_edit',   'large_attachment_fields_to_edit' , 0,  2);
function large_attachment_fields_to_edit($fields, $post){
    if (substr($post->post_mime_type,0,5) == 'image'){
            $html = "<input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr(array_shift( wp_get_attachment_image_src($post->ID, 'large', false) )) . "' /><br />
                            <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
                            <button type='button' class='button urllarge' data-link-url='".esc_attr(array_shift( wp_get_attachment_image_src($post->ID, 'large', false) ))."'>Large File URL</button>
                            <button type='button' class='button urlfile' data-link-url='" . esc_attr(wp_get_attachment_url($post->ID)) . "'>" . __('Original File URL') . "</button>
                            <button type='button' class='button urlpost' data-link-url='" . esc_attr(get_attachment_link($post->ID)) . "'>" . __('Post URL') . "</button>
            $fields['url']['html'] = $html;
    return $fields;


Your Answer

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

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