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);
Will
January 22nd, 2012 at 9:21 pm
I wrote a plugin that allows you to remove the link altogether (I wanted to do the opposite of what you had done here) and I found your post very helpful when researching what I should be hooking into in WordPress. Thank you!
My plugin is at http://techblog.willshouse.com/remove-link-url/ 🙂
Isabel
January 26th, 2012 at 11:14 am
Thanks for your comment. I see how your plugin is also useful–will use it in the future on another blog.
Gabe Morton-Cook
February 29th, 2012 at 11:10 pm
Hi,
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.
shining
September 21st, 2012 at 3:14 am
thank for the code.