

WordPress automatically creates attachment pages for every media file you upload, but these pages often serve no real purpose. This code snippet solves that problem by disabling those attachment pages automatically.
function redirect_attachments() {
if (!is_attachment()) {
return;
}
$attachment = get_post();
$parent_id = $attachment->post_parent;
$parent_post_status = get_post_status($parent_id);
$parent_permalink = get_permalink($parent_id);
$destination = home_url();
$http_status_code = 302;
if($parent_id && $parent_post_status !== 'trash') {
$destination = $parent_permalink;
$http_status_code = 301;
}
wp_safe_redirect($destination,$http_status_code);
exit;
}
add_action('template_redirect', 'redirect_attachments', 1);
Follow these simple steps to disable attachment pages in WordPress:
That’s it! Attachment pages are now disabled on your WordPress site.
WordPress attachment pages are dedicated pages automatically created for each media file uploaded to your site, including images, videos, PDFs, and other documents. When you upload a file through the Media Library, WordPress assigns it metadata similar to standard posts, including a title, author, publication date, and unique URL.
These attachment pages function as individual showcases for your media files, similar to how post pages display blog posts. They can display the media file along with additional information like titles, captions, descriptions, and even allow visitors to leave comments on the media itself.
While attachment pages might seem useful, they often create significant SEO problems that can hurt your search rankings. The main issue is that these pages typically contain what search engines classify as “thin content.”
Attachment pages often trigger duplicate content issues because the template elements, like headers, footers, and sidebars, outweigh the actual unique content on these pages. When attachment pages are indexed alongside their parent posts, search engines may view them as redundant pages competing for the same keywords.
Another problem is that when you have hundreds or thousands of attachment pages indexed, it can significantly dilute your site’s overall authority and rankings.
Disabling attachment pages prevents the above SEO issues from occurring in the first place. When you redirect attachment pages to more meaningful content, you ensure that search engines only index pages with substantial, valuable content.
The code snippet provided intelligently redirects attachment pages to their parent post if one exists, using a 301 permanent redirect. This passes SEO value to the parent content rather than wasting it on thin attachment pages. If an attachment doesn’t have a parent post, it redirects to your homepage.
This approach also improves user experience. When someone lands on an attachment page, they’re more likely to find valuable content on the parent post rather than just seeing a lone image with minimal context.





