Disable Image Attachment Pages in WordPress: A Guide

- Advertisement -

How to Redirect Image Attachment Pages in WordPress for Better SEO

During a recent on-page SEO clean-up of my website, I noticed that Google had indexed multiple “image pages.” These pages are created automatically when you add an image to a post on WordPress, and they contain no content other than the image itself and perhaps a title and caption. Since these pages are considered “thin” content, they hold no value and can actually harm your SEO efforts. In this article, I’ll show you how to disable these attachment pages and redirect your page rank back to your primary article or homepage.

- Advertisement -

Redirecting Image Attachment Pages with the Yoast SEO Plugin

The easiest way to redirect your attachment pages is by using a plugin. Yoast SEO for WordPress is a free plugin that can help you with this. Here are the three easy steps:

- Advertisement -

1. Install and set up Yoast SEO for WordPress.

2. Once set up, click on SEO > Advanced and click on the Permalinks tab.

- Advertisement -

3. Enable the option to “redirect attachment URLs to parent post URL” and save.

Redirecting via Custom Function

You can also create your own custom function for redirecting your attachment pages to either the homepage or the post parent. This function should be placed in your functions.php file (preferably in a child theme if you are using a theme by another developer).

function myprefix_redirect_attachment_page() {

if ( is_attachment() ) {

global $post;

if ( $post && $post->post_parent ) {

wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );

exit;

} else {

wp_redirect( esc_url( home_url( ‘/’ ) ), 301 );

exit;

}

}

}

add_action( ‘template_redirect’, ‘myprefix_redirect_attachment_page’ );

If you notice, the code will only redirect when is_attachment() returns true. This means it will redirect for any attachment type, not just images, which is usually best. However, you can target only image attachments by adding some extra checks to see what the current attachment type is.

Redirecting via the image.php File

Your second option is to completely disable your attachment pages by adding a WordPress redirect directly to the top of your image.php file.

1. Create a new file called image.php in your child theme (always best to work with child themes when editing a theme) or if you are developing your own theme, create an empty image.php file in your theme.

2. Insert the code below in your image.php file:

global $post;

if ( $post && $post->post_parent ) {

wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );

exit;

} else {

wp_redirect( esc_url( home_url( ‘/’ ) ), 301 );

exit;

}

Now when you refresh your image attachment page, it should redirect to the original post where the image was uploaded, or if it was uploaded directly in the media library, it will redirect to your site’s homepage.

Conclusion

By redirecting your image attachment pages, you can improve your website’s SEO and ensure that your visitors are directed to the content that matters most. Whether you choose to use a plugin, create a custom function, or add a redirect to your image.php file, taking this step can help you maximize the value of your website’s content and boost your search engine rankings.

- Advertisement -

Stay in Touch

spot_img

Related Articles