PHPMatters Help You Better Hosting Your PHP-based Sites
How to Stop Search Engines Indexing WordPress Image Attachment Pages

How to Stop Search Engines Indexing WordPress Image Attachment Pages

If an image is uploaded to website and included in a page/post, it automatically becomes an attachment for that page/post. That is image attachment. WordPres image attachment page is where to show a certain image alone rather than display all content on this page. By default, visitors are able to search for this single page and land on it if attracted to that.

However, since Google gets started to penalize the sites with too many unnecessary pages, it drives webmasters to exclude needless content for better SEO. WordPress attachment page is included. Here, we make this guide on how to stop indexing WordPress image attachment pages along with the way to direct image page to respective post. The methods selected in this article include the use of coding stuff and plugin.

Add a Line of Code to Your Theme

Log into your WordPress dashboard and go to Appearance > Editor and check if there is an image.php file included in the active file. If not, you are required to create a new file under this theme and name it as image.php. Given that cPanel is used for our website management, we are going to do so via this powerful control panel.

Access to cPanel with your own account and target Files > File Manager. When coming to File Manager interface, you should search for the active theme by following public_html/subdomain (or leave it blank)/wp-content/themes/the active theme name. And then, click New File button on the top of this page. Enter image.php in the “New File Name” field and “Create New File” to confirm the changes.

create image.php file

Right click the newly created image.php and access to the edit page. Add the following line of code to the editor and “Save” it to make the code into effect. Perhaps, you can go back to your WP admin > Appearance > Editor and check if there is a file called Image Attachment Templates (image.php) included in the Templates list. If so, open it and add the code below to this file. The last step is to click “Update File” button.

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

Since everything is OK, go to Media > Library and pick out an image attachment as your own choice. Click to enter the Attachment Details page and “View attachment page”. If it is linked to the parent post, you have successfully stopped indexing image attachment page.

attachment details page

Use WordPress SEO by Yoast

As one of the best SEO plugins, WordPress SEO by Yoast is the pitch on that helps you reach the target without using coding stuff. And now, let’s start with the plugin installation and activation via Plugins > Add New. Search for WordPress SEO by Yoast and get the latest version. Click “Install Now”. The installation is not completed until a new item called “SEO” appears in the sidebar.

install WordPress SEO by Yoast

Go to SEO > Permalinks and access to the Permalink Settings page. There are 6 options available for you to customize the permalinks, among which the forth one “Redirect attachment URL’s to parent post URL” is the one you are looking for. It enables attachments to be linked to respective parent pages or pages. “Save Changes” to confirm all settings.

seo permalink setting page

In this way, the image indexed by search engines would come with a link to the post where shows it accordingly. If visitors seek this image out through Google, then they could click to the post where displays this image with the given link.

Given that WordPress SEO by Yoast is a powerful plugin with multiple features but some of you only need a single of function, we recommend some other plugins specialized in attachment page redirect, such as Attachment Pages Redirect, Noindex Attachment Pages, WP SEO Redirect 301, and so on so forth. All those options help to redirect pages for better information showcase.

Having had a rough idea about how to prevent image attachment page from indexing, you should get started to modify your image attachment redirect right now. We hope everything will go swimmingly.