Okay, so you have WordPress setup to display all of your images using your favorite lightbox-inspired image viewer. Now the only problem is that WordPress only gives you the option to link the image to the post, or link the image to the full file. In order to link to the large file size—which is what you ultimately want—you must manually edit the HTML inserted into your post. Unacceptable!
I set out to find a simple plugin, to no avail. I figured I wasn’t the first person to come across this issue. Since I couldn’t find exactly what I was looking for, it was time to write a custom function.
I dug around some of the core files and determined the code I wanted to change was the admin function image_link_input_fields(), located under wp-admin/includes/media.php. I wanted the default URL to be the large image, and possibly while maintaining the ability to choose None, Original File, or Post.
Modifying the core file is not advisable, since you would have to re-apply changes every time you update WordPress. Filters to the rescue!
The Solution
Here’s my solution. Simply add this to your theme’s functions.php file:
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' title=''>" . __('None') . "</button> <button type='button' class='button urlfile' title='".esc_attr(array_shift( wp_get_attachment_image_src($post->ID, 'large', false) ))."'>Large File URL</button> <button type='button' class='button urlfile' title='" . esc_attr(wp_get_attachment_url($post->ID)) . "'>" . __('Original File URL') . "</button> <button type='button' class='button urlpost' title='" . esc_attr(get_attachment_link($post->ID)) . "'>" . __('Post URL') . "</button>"; $fields['url']['html'] = $html; } return $fields;}
This will set the default URL for images to be the large image size, instead of the full image size. It also adds buttons for None, Large File URL, Original File URL (full), and Post URL.
How it works
Basically, the only useful hook I could find was attachment_fields_to_edit. It would allow me to modify the HTML generated on the “Add an Image” admin screen, right before the user inserts the image into the post. Since I only wanted to change the URL field, I replaced $fields[‘url’][‘html’] completely.
The first part is the input field itself. I wanted to set the default value to the large image. I used wp_get_attachment_image_src() to pull the URL for the large image.
<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) )) . "' />
Next, I simply copied some code from the WordPress core file to re-add the default buttons, along with another one for the large image.