Disable WordPress attachment pages

Published by マギルゥーベルベット on

I’m still pretty new to WordPress and find new stuff every day. Today, by accident, I found a feature of WordPress named “Attachment Pages”. I was instantly annoyed by this, as I see no reason to have such links on a tech website/blog. I see the point for photographs and media blogs, but not for tech websites. After some research I couldn’t really found something useful, other than redirecting these pages to their parent post. I didn’t want that either. What I wanted to do was to trigger an 404 error instead and block those pages completely for good. After some inspiration of some existing plugins and code snippets I hacked my own one together.

Plugin Name: Attachment Pages 404
Description: Makes attachment pages trigger a 404 error.
Author: マギルゥーベルベット
Version: 1.0
Author URI: https://magiruuvelvet.gdn
if (!defined("ABSPATH"))

function sar_attachment_404()
    if (is_attachment())
        wp_die("Page not found.", "404 - Page not found", 404);

add_action("template_redirect", "sar_attachment_404", 1);

Install it as a plugin or use the PHP code snippets (Insert PHP) plugin and create a new snippet which runs everywhere. After its activation, admins, users and visitors can no longer view attachments by their standalone URLs and must be accessed using the full /wp-content URL.

For the lazy I prepared a ZIP file. Use the dashboard to upload and install it.