Disable Attachment Pages in WordPress (Code Snippet) and Boost Your SEO

illustration of flying rocket ships.


In today’s digital age, having a strong online presence is crucial for businesses and individuals alike. WordPress, being one of the most popular content management systems, offers a wide range of features and functionalities to enhance your website’s performance. However, there is one aspect that can negatively impact your SEO efforts – attachment pages.

Attachment pages are automatically generated by WordPress when you upload media files such as images or videos. These pages display the media file along with some additional information. While they may seem harmless, attachment pages can actually dilute your website’s content and confuse search engines. In this article, we will explore the benefits of disabling attachment pages and provide you with a step-by-step guide to achieve the best results.

The Benefits of Disabling Attachment Pages

1. Improved SEO Focus

By disabling attachment pages, you can ensure that search engines focus solely on your main content. Attachment pages often contain duplicate content, which can lead to keyword cannibalization and dilution of your website’s SEO efforts. By eliminating attachment pages, you can consolidate your content and increase the relevance of your primary pages in search engine rankings.

2. Enhanced User Experience

Attachment pages can disrupt the user experience by diverting visitors away from your main content. When users click on an image or media file, they expect to view it within the context of the page they are currently on. Attachment pages can create confusion and make it difficult for users to navigate back to the original page. Disabling attachment pages ensures a seamless browsing experience for your visitors.

3. Faster Page Load Times

Each attachment page generates additional HTTP requests, which can slow down your website’s performance. By disabling attachment pages, you can reduce the number of requests and improve the overall page load times. This not only enhances user experience but also contributes to better SEO rankings, as page speed is a crucial factor in search engine algorithms.

The Downsides of Disabling Attachment Pages

While disabling attachment pages offers numerous benefits, it’s important to consider the potential downsides as well. Here are a few points to keep in mind:

1. Loss of Image Search Traffic

Attachment pages can sometimes rank well in image search results, driving traffic to your website. Disabling attachment pages means that these specific pages will no longer be indexed by search engines, potentially resulting in a loss of image search traffic. However, this can be mitigated by optimizing your main content for image search and utilizing alternative methods such as image sitemaps.

2. Impact on Internal Linking Structure

If your website heavily relies on internal links to attachment pages, disabling them can disrupt your internal linking structure. It’s important to review and update your internal links to ensure they point to the appropriate main content pages. This may require some manual effort, but the long-term benefits outweigh the temporary inconvenience.

How to Disable Attachment Pages in WordPress

Now that we understand the benefits and potential downsides of disabling attachment pages, let’s dive into the practical steps to achieve the best results. Follow the guide below:

  1. Backup Your Website: Before making any changes to your WordPress settings or code, it’s crucial to create a backup of your website. This ensures that you can revert back in case anything goes wrong during the process.
  2. Install and Activate the Code Snippets Plugin: The Code Snippets plugin allows you to add custom code snippets to your WordPress website without modifying your theme’s files. Install and activate the plugin from the WordPress plugin directory.
  3. Create a New Snippet: Once the plugin is activated, navigate to “Snippets” in your WordPress dashboard and click on “Add New”. Give your snippet a descriptive title, such as “Disable Attachment Pages”.
  4. Add the Code: In the code editor section, paste the following code snippet:
function disable_attachment_pages() {
    global $post;

    if (is_attachment()) {
        wp_redirect(get_permalink($post->post_parent), 301);
add_action('template_redirect', 'disable_attachment_pages');

This code snippet checks if the current page is an attachment page and redirects it to the parent page with a 301 redirect, indicating a permanent move. This effectively disables attachment pages and redirects users and search engines to the appropriate main content.

  1. Save and Activate the Snippet: Click on the “Save Changes and Activate” button to save your snippet and activate it on your website.
  2. Test and Verify: Visit one of your attachment pages to ensure that it now redirects to the parent page. Additionally, check your website’s SEO performance and monitor any changes in search engine rankings and traffic.

Congratulations! You have successfully disabled attachment pages in WordPress, improving your SEO focus and enhancing user experience.


Disabling attachment pages in WordPress is a crucial step towards optimizing your website for search engines and providing a seamless browsing experience for your visitors. By following the steps outlined in this guide, you can eliminate duplicate content, improve page load times, and consolidate your SEO efforts. Remember to regularly monitor your website’s performance and make necessary adjustments to ensure optimal results.

We hope this guide has been helpful in understanding the benefits and process of disabling attachment pages. For more WordPress information, tips, and code snippets, visit PrimaryWP. Good luck with your SEO endeavors!

Customize Code

Try an AI WordPress Developer for Free

Do you need your code snippets to do something specific? Don’t worry you can use AI.

Email Icon

Get WP snippets in your inbox

Be one of the first to know about new PrimaryWP updates & code snippets for WordPress.


Leave a Reply

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