How to stop WordPress attachments appearing in search results

This is a really quick tutorial because it has been annpying me more and more recently for various WordPress client websites.

The problem is simple – WordPress creates page called an “Attachment Page” for every image you upload.  Its so unnecessary, but these pages can sometimes crop up in search engine results.

We can stop this however, and with a really quick piece of code.  All we are doing below in “noindexing” the attachment pages so they are not indexed when Google crawls our websites…simple!

Just drop the code below into your functions.php file and save, and you should be all good…


function attachmentpages_noindex() {
if(is_attachment()) {
echo '';
add_action('wp_head', 'attachmentpages_noindex');