Connect with us

How to redirect the attachment page to the attachment file?

solution

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);
        exit;
    }
}
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:

update_option('image_default_link_type','file');

 

Related:  Fixing WordPress Fatal error: Class WP Post_Type not found

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
You may also like...

We are a WordPress plugin developer company that focuses on useful WordPress plugin creation and empowering people to earn passive incomes from their blogs. We build unique and groundbreaking plugins that will revolutionize your blog!

Click to comment

Leave a Reply

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

More in solution

    Sign up for our newsletter and get a free affiliate e-book!

    About Me:

    Szabi Kisded

    Hi, my name is Szabi and I'm documenting my journey selling plugins on CodeCanyon. I will show you every step of it: learning to code, plugin ideas, WordPress stuff and more. Read more…

    Mega Plugin Bundle:

    CodeCanyon Portfolio:

    Online Courses:

    Latest Promotions:

    Recommended Theme:

    Start Your Own Blog:

    Translate Your Blog:

    AdSense Alternative:

    Best Article Spinner:

    Popular Posts:

    Latest Posts:

    To Top

    Privacy Preference Center

        Necessary

        Advertising

        Analytics

        Other