6

I would like to disable all attachment pages completely. I Googled it, but there's just information on how to redirect to parent post or homepage. That's not what I would call an elegant solution. Why introduce unnecessary permalinks that redirect to the homepage? Couldn't it be disabled completely?

  • 1
    What do you mean by "disabled completely"? When adding media attachment URLs are created automatically. I don't know if you could disable this action without harming Wordpress core. Why isn't redirecting an elegant solution? What is the problem with the attachment URLs? I'm not judging, just trying to understand so I can offer a solution. – Fencer04 Aug 31 '16 at 17:35
  • 2
    It just seems pointless to me that every file I upload gets its own page with a permalink. I can't think of many use scenarios for them. Most people just want to upload files to link to them, not creating a separate page to display them. – Maciej Krawczyk Sep 1 '16 at 5:19
  • 1
    @Fencer04 it can be done without harming WP core, see my reply. In many cases it makes sense. Redirects are bad and technically it's not the proper use case for them - instead of redirecting existing URL you better don't have that URL in the first place if you don't use it. Also, on every request WP scans all rewrite rules array until finds the first match, it evaluates them using regular expressions, which is slow. Smaller array or rules means better performance. For this reason, I often remove dates, author archives, embeds, feeds etc - basically all features that aren't used on the project. – Ihor Vorotnov Jun 23 '17 at 10:14
9

You can filter default rewrite rules and remove those for attachments:

function cleanup_default_rewrite_rules( $rules ) {
    foreach ( $rules as $regex => $query ) {
        if ( strpos( $regex, 'attachment' ) || strpos( $query, 'attachment' ) ) {
            unset( $rules[ $regex ] );
        }
    }

    return $rules;
}
add_filter( 'rewrite_rules_array', 'cleanup_default_rewrite_rules' );

Don't forget to re-save your permalinks once. WordPress will generate new rules without anything related to attachments.

Now, the attachment page URL gives 404. You can also add that redirect to prevent the 404 page, it's useless in this case. But I'm not sure how to catch the redirect - is_attachment() will not work if the rewrite rules are removed.

Update:

WordPress will still offer the attachment page pretty URLs in media library and media insertion dialog. You can filter this as well:

function cleanup_attachment_link( $link ) {
    return;
}
add_filter( 'attachment_link', 'cleanup_attachment_link' );

In this case, even when you insert your attachment into post ans select "Link to attachment page", the image will be inserted without the link.

| improve this answer | |
  • Would it be possible to set the rewrite to be the media's File URL (linked to wp-content/uploads) instead of an auto-generated page? – italiansoda Jul 13 '17 at 17:25
  • Yes, I would like to have the possibility to redirect all attachment pages to the file url as well :) – Ola Oct 2 '18 at 9:21
  • The Yoast SEO plugin handles this perfectly. – wp-mario.ru May 8 at 13:01
  • By the way, this code does not work completely. If the attachment is loaded for a post or page, then everything works fine. If you just upload a attachment to the media library, its page still opens. – wp-mario.ru May 8 at 13:06
  • @Mario62RUS 1. Not everyone uses Yoast SEO. Some people do hate the bloat it brings. It's like cancer for your admin UI. 2. Yes, things have changed since 5.0 and Gutenberg. This code should be revised. – Ihor Vorotnov May 11 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.