Connect with us

How to redirect the attachment page to the attachment file?


How to redirect the attachment page to the attachment file?

By default WordPress links images that are inserted into a post to their attachment page URL.  This page can have its own template within WordPress and is just like any other page on your site.  The issue is most themes and blog owners don’t account for the attachment page and it ends up looking odd, broken or redundant.  Another issue is that it can get considered as duplicate content and thin content by Google in terms of SEO.  So with that said there are two things you need to do to both correct posts that already have the attachment URLs embedded in them and also make sure the issue doesn’t persist with newly created posts.

First, let’s work on getting the current posts with attachment page links in them to 301 redirect to the actual attachment file instead of the attachment page.  A 301 redirect tells Google and everyone else that the content has moved for good.  Add the following code to your functions file:

function coderevo_attachment_redirect()
    global $post;
    if ( is_attachment() ) {
        wp_redirect(wp_get_attachment_url( $post->ID ), 301);
add_action('template_redirect', 'coderevo_attachment_redirect',1);


The second thing you will want to do is configure WordPress so that future images inserted into posts will either link directly to the files or not link to anything at all.  The easiest way for the do-it-yourselfer is to just add the following code to your functions.php file.

update_option( 'image_default_link_type', 'none' );


This will update WordPress to set the default to have no link when you insert an uploaded image into a post.  The other option is to have it link to the uploaded file itself.  You can do this by the following code:



Note: you really only need to add this into your functions.php file for a short while as it updates the setting on the next page load.

And that’s it!

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

More in solution

    About Me:

    Szabi Kisded

    Hey there, I'm Szabi. At 30 years old, I quit my IT job and started my own business and became a full time WordPress plugin developer, blogger and stay-at-home dad. Here I'm documenting my journey earning an online (semi)passive income. Read more

    Sign up for my newsletter and get the YouTube Caption Scraper WordPress plugin for free
    (worth 29$)!

    All My Plugins In A Bundle:

    My AutoBlogging Plugins:

    My Online Courses:

    A Theme I Recommend:

    Featured Posts:

    To Top