Force Images to Link to URL (Full-Size Image), Disable Attachment Page

Override the WordPress image-upload settings that allow you to choose to link an image to it’s full-size file url or to an attachment page (Post URL). This code will force all images in your posts to be linked to their original file. This is useful if you use WordPress for clients who don’t want to be bothered with those settings. If you’re using a slideshow or lightbox display for images, such as Pretty Photo, you absolutely need your pictures to link to their original full-size file, never to an attachment page. You don’t want your clients messing that function up by accidentally choosing to link a picture to an attachment page. This code is the answer for such a problem. This code forces images to link to their file url, but also removes the “Link URL” choice from the image-upload box. The default choices for “Link URL” are None, File URL, or Post URL, in which Post URL is the nasty attachment page. This choice will be removed with the following code. Finally an easy way to disable the attachment page for images!

One Caveat

The code works for all future posts starting from when you install the code. It will not do anything to the image links of past posts. If you’re stuck with posts that have images linked to attachment pages right now, and if you want to change those links to the picture, then you must go in to each post and manually choose “File URL” from the “Link URL” choice for each picture. Do so before adding this code, since this code will remove the “Link URL” choice altogether. You will not be able to change link settings after this code.

And now the code:

Place following code in the functions.php file of your WordPress theme. Remember to make sure the code is enclosed in php tags.

function custom_attachment_fields_to_edit($form_fields, $post) {

    $form_fields["image_url"] = '';
    $form_fields["url"] = '';

    $form_fields["url"]["input"] = "hidden";
    $form_fields["url"]["value"] = wp_get_attachment_url($post->ID);

    return $form_fields;
add_filter("attachment_fields_to_edit", "custom_attachment_fields_to_edit", null, 2);

See more: ,

We've 4 Responses

  1. February 29th, 2012 at 11:10 pm


    Thank you for this code snippet, this is a simple, elegant solution. I’m actually trying to get it to work the opposite way, so that images always link to the attachment page rather than the image file.

    I’ve managed to do so by changing line 7 as follows:

    $form_fields[“url”][“value”] = get_attachment_link($post->ID);

    This is working perfectly for the add new image dialog, but I’m still seeing the Link URL field (with buttons for “None”, “Current Link” and “Link To Image”) when editing an image.

    Note: this is not the pre-placed images caveat that you explained. This is when I open the image edit dialog on any image, even one that was placed after implementing this code.

    Does all that make sense? Any ideas?

    Thanks ahead of time.


Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: