I recently answered a support question on one of the WordPress question and answer sites about forcing pdf links to download in Acrobat or another pdf application, rather than opening in the browser window. I came up with what I thought was a pretty elegant solution, which involved creating a pdf.php template which set the HTTP headers necessary to force a download, rather than allowing the browser to attempt to render the file.

The Link URL field on uploading an image defaults to the File URL, leading to annoying behavior when you click the image (try it and see).

(This is especially important when linking to large pdf files… linking to a 10MB manual without warning your users to right-click and save it will crash many browsers… and even if the browser can handle it, it doesn’t have all the features available in the Acrobat application.)

As I thought about it, though, this relates to a bigger problem with the way attachments are generally handled in WordPress sites. When you add an image through the media uploader and insert it into a blog post, the default “link url” setting is to the image file itself (see screenshot to the right). That makes sense for bloggers writing photo-heavy blogs, who may insert a large photograph and display the “large” or “medium” size in their post, but have the original image linked to their post. But far too often I visit blogs and find a link on an image, only to click on it and be taken to the exact same image, on a white screen, with no navigation or text… in short, a wasted link.

One Solution

Change the default link behavior for images to “none” in your site options

The default link type is stored in a hidden option in your site. I found a good simple tutorial for finding and changing this option on Strange Symphonies.

In short: visit {yoursite.url}/wp-admin/options.php. This hidden page on the WordPress dashboard will show you all the settings in the wp-options table of your database. Find the image_default_link_type option. By default this is set to file, but you can change it to none or post. Setting this option to none will get rid of a lot of annoying links-to-nowhere on your site. (Just remember to link to the file or post when you actually want that behavior).

A minor UI problem with this technique, though: the image_default_link_type option affects media uploads as well. If you change this option, then your media uploads will also default to no link. This minor hack in your functions.php will put the file link back in for media files:

add_filter( 'attachment_fields_to_edit',
	'attachments_link_to_file', 10, 2 );
function attachments_link_to_file( $form_fields, $post ) {
	// if its an image, don't do anything
	if ( stristr( $post->post_mime_type, 'image' ) )
		return $form_fields;
	// if a link is already specified, don't bother
	if ( preg_match( '/value=\'\S+\'/',
		$form_fields['url']['html'] ) )
		return $form_fields;
	$form_fields['url']['html'] = preg_replace('/value=\'.*\'/',
		"value='".esc_attr( get_attachment_url($post->ID) )."'",
		$form_fields['url']['html'] );
	return $form_fields;
}

This is really just the tip of the iceberg, and not really what I started out wanting to talk about. I’ll post a followup soon with some more ideas (hint: they involve always linking to “Post URL” and letting the attachment.php template do the work of sorting out the display options.)

An Update

Did some more thinking about it and realized how petty and hackish this approach is. Use it if it makes sense to you, but it would be much more effective to add another option or filter into core. I opened a trac ticket proposing that an additional option be added to WordPress for media_default_link_type — if you have thoughts on a better way to handle this, feel free to weigh in there. Adding an additional option into core for something as trivial as this is probably not going to happen, but there has got to be a better way of handling upload links than is currently being done.

Leave a Reply