disable image attachment pages

How to disable image attachment pages in WordPress

Are you trying to disable the image attachment pages in WordPress?

WordPress creates a single page for every media file by default. So every media attachment on your website has its own separate page. But it looks like an incomplete page which can give a negative impression to the users. Sometimes website owners link their image to the attachment pages but it doesn’t look good.

If your theme doesn’t have the image attachment pages so you need to disable it manually. Sometimes your website any image can get popular and users land on the image page but users should land on the post that contains the image.

Disable image attachment pages in WordPress

To disable the image attachment page you can use the All in One SEO WordPress plugin. It is the easiest way to disable all the image attachment pages.  Install and activate the plugin you can see the All in One SEO option in your WordPress dashboard.

Go to All in One SEO > Search Appearance and then the media tab.

disable image attachment pages

In the Media tab, you can see the Attachment parent option. Enable it so that when users land on the image page it will redirect to the content page instead. Click on the Save changes button to save the settings.

There is also a plugin named Attachment Pages Redirect to redirect the attachment page. Install and activate the plugin. Through this plugin, you can easily enable the setting. It’s very simple and easy.

Add image attachment without plugin

If you don’t want to use a plugin then you can add code snippets to disable the image attachment. At first, create a file named image.php in your WordPress theme folder. After that add the following code to that image.php file.

<?php wp_redirect(get_permalink($post->post_parent)); ?>

Now save the file and upload it to your WordPress theme directory.

This little code snippet will automatically redirect the users to the main post.

We can also see our other tutorial to learn How to create a WordPress popup based on Location.

How to Create a Separate Page for Blog Posts in WordPress

We hope this article will help you to disable the image attachment page and redirect it to the main post.