It can be a great idea to disable and redirect all the attachments posts in WordPress to the parent post where image is used,WordPress by default creates separate attachment page for every image you upload even is it’s attached to some post on your WordPress site which is sometime annoying as you will not like that users browse that image in a separate page which is attached to some post,therefore the only way to get rid of this is to disable the attachment pages in your WordPress blog and redirect them to the post where image is originally used.
You can disable and redirect attachment pages to parent posts in two ways, one by using some plugin or the second one by adding appropriate codes if you don’t like to add unnecessary plugins just for a simple task to disable the attachment page and redirect the attachment to the Parent page(where image is originally used).
Disable Image Attachment Posts In WordPress
Using a Plugin-
If you are using Yoast SEO plugin you can easily disable and redirect the attachment page to parent page to do this follow below steps.
1.Open your WordPress dashboard.
2.Navigate to SEO tab and Permalinks.
3.Now here you will find the option “Redirect attachment URL’s to parent post URL.” this will disable the Image attachment page and will redirect them to original posts where images are being used just enable it and save the settings.
Disabling Image Attachment Pages using Codes-
1.Create a new .php page(You can see in the end how to create a .php page) and name it whatever you want to.
2.Paste the below code in it.
<?php wp_redirect(get_permalink($post->post_parent)); ?>
3.Now upload it to the theme folder of the theme which is active now(WP-Content>Theme>Theme name).
How to create a .php Page?
1.Open any text editor like Notepad if on Windows.
2.Paste the above code in it.
3.Now Click on Save as and choose the mode “All Files” and now give the file a name and after that add .php(eg file name-wpcub.php) remember don’t save it as txt file.