5 Ways To Change Default Image Attachment Links

Every time you upload an image, WordPress generates another post for that media file which is loaded using the attachment.php template.

WordPress also creates multiple sizes of every image you upload according to your media settings and custom image sizes added to your child themes functions.php file.

So what does this mean?

It means your uploads folder is going to contain at least 4 sizes of every image you upload (Go to cPanel or FTP and take a look).

It also means media items (files) are also ‘Posts’ in their own right and WordPress has created a post for every media file you have uploaded.

If you want to change the default image link type from attachment page to file URL or post URL, here’s a few ways to get the job done.

Image Link Options When Uploading

You now have 4 options to choose from when uploading new images to posts and pages in WordPress.

Choosing the none option results in your images linking to the posts they where uploaded to which is one of the best options.

Link Image To

Changing Default Image Link Type

Changing your settings manually.

You can actually access your wp-options file by typing in yourdomain/wp-admin/options.php in your browser and logging into your site. This will give you access to all settings where you can find:

image_default_link_type

all settings-image_default_link_type

In the field next to this setting, you have 3 choices:

  1. none – This links back to the post it was uploaded to.
  2. file – This links to the image file in your wp-content/uploads folder. (Not recommended as may be used for hotlinking)
  3. post – This links back to the post it was uploaded to.
  4. attachment – This links to an attachment post generated by WordPress for all image files uploaded to a post.

Personally. i think its best to select post or none so your images can’t be hot linked and click thru to your content when indexed in the search engines.

Set default values for Media Uploads

Another way to fix the default image link type for single images uploaded to a post is to add this code to your child themes functions.php file.


You can change instances of post to none or file depending on your own needs.

Attachment Display Settings

This solution only works on new images uploaded to single posts and pages.

To redirect all images from attachment pages to your post or file URL’s, install a plugin.

Redirect Image Attachment URLs to Parent Post

Another option and the last resort if you’re not already using this plugin is to install Yoast’s excellent All In One SEO plugin and use the permalink settings to Redirect attachment URL’s to parent post URL.

permalink settings

Conclusion

One of the main reasons you might want to consider redirecting your attachment posts for images back to the parent post is that they are generally low in content and could negatively effect your rankings if you allow them to be indexed.

Related Tutorials


Comments

7 responses to “5 Ways To Change Default Image Attachment Links”

  1. Is there anyway to redirect media files in pages to their attachment page?

    1. Brad Dalton Avatar
      Brad Dalton

      I think you could add a conditional tag after the function for pages and that might work.

      1. I’m still very new at this, insert a conditional tag for every page with the media file links? And what would that condition tag be?

        Very much appreciate your help Brad.

  2. Jonas Lundman Avatar
    Jonas Lundman

    This is not working anymore, the options must be set within after theme setup function

    function mytheme_setup() {
    // Set default values for the upload media box
    update_option(‘image_default_align’, ‘center’ );
    update_option(‘image_default_link_type’, ‘none’ );
    update_option(‘image_default_size’, ‘large’ );
    }
    add_action(‘after_setup_theme’, ‘mytheme_setup’);

    And the versions btw 3.1 – 3.7 have some bugs that can be worked around, see this post:

    http://wordpress.org/support/topic/image_default_link_type

    1. Brad Dalton Avatar
      Brad Dalton

      Yes. Thanks for the update Jonas.

      Looks like changes in WordPress have caused this to stop working however there’s always other options.

      I know there are bugs which are 5 years in WordPress including linking gallery images to custom urls.

  3. Danny Cruz Avatar
    Danny Cruz

    Hi Brad, this is excellent. Thanks for sending me the link. Like I said on the Studiopress forum, I’m sticking to Yoast for now. I really like the extra functionality over the stock Genesis SEO features. If it doesn’t work out in the end, I can easily switch back if need be with minor work to convert the SEO options back over.

    One thing I need to point out though. The option you mention above about linking the images to “none” is fine, but it won’t work for people like me that have large version of the images appear in a lightbox after a click.

    I used to turn off image resizing in WP and upload every size of image I needed individually. This yields much better quality as I can control the resizing in Photoshop. But it quickly became a hassle and a time suck. WordPress seems to have become better at resizing, so the quality doesn’t suffer much anymore. So nowadays, I’ve been uploading a 1140px image for each photo, from an optimized for web original, and letting WP resize for main image and thumbnails.

    However, since I have to link to the original image in order to display the large image in a lightbox or carousel gallery upon a click, I still have to link to them and can’t use the “none” option.

    1. Brad Dalton Avatar
      Brad Dalton

      Good point on the linking. I normally link to the image file. Not sure why i mentioned linking to none so i’ll have to refresh my memory and update the post.

      I now upload 600 x 300 as featured images and use smaller custom sizes in functions.php like 300 x 100 etc for thumbnails so this maximizes the quality when resizing. I guess the best size would be the same width as the header.

      Thanks for the quality comments Danny!!

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.