How To Redirect Image Attachment Pages In WordPress

The way that WordPress deals with image attachments can cause unintended issues – both for visitors to the site and for SEO purposes.

I have included at the bottom of this article a possible code snippet to fix such issues but first, let’s consider an example of what happens when you insert images in a WordPress post:

E.g. you wrote a post called ‘newpost’ and insert 2 images into it using a link to the attachment post (WordPress default) – pic1 and pic2. The following URLs may be produced when you publish the post:

Yoursite.com/newpost – all content including your text and the two embedded images. This is the ‘parent’ post which you want your visitors to see – and search engines to index.

Yoursite.com/newpost/pic1
Yoursite.com/newpost/pic2
Yoursite.com/?attachment_id=1
Yoursite.com/?attachment_id=2

These 4 URLs are ‘child’ attachment posts which contain no content apart from the image itself – and those images are simply duplicates of the ones embedded in the main newpost anyway. They are clearly not great pages for visitors as the image may mean nothing when taken out of context (as there is no text to explain it).

Even worse, if your site offers social media buttons you may find that visitors mistakenly like/tweet the image post instead of the main post – very confusing when their followers visit the link and find only a single image with no article text…

Image attachment pages may also (in theory) lead to some search engines marking your site down for having lots of ‘thin’ content – as well as the main newpost you have 4 other posts containing nothing except an image – and a duplicate image at that.

One more issue may arise – image attachment posts are not deleted if you delete the main post, even if you delete the images from your upload folder and/or WordPress media gallery i.e. if you delete newpost it will 404 (page not found) in future but the 4 image posts will remain as ‘orphans’ – published, indexed and possibly with inbound links, even though the parent post and all the text in it no longer exists.

Fixing Image Attachment Issues

There are various ways to fix these issues but I decided to use the following logical steps:

1. Make images in main posts non-clickable i.e. prevent visitors clicking through to the ‘no-content’ image posts. This stops direct click navigation but it is still possible for indexed image posts to be visited via Google Image Search etc or from inbound links so the next steps are also required.

2. For all traffic to ‘child’ image posts, redirect to the respective ‘parent’ main post. This resolves the previous issue of external traffic linking directly to the image post – it will now be sent to the parent instead.

3. If a main post has been deleted, its child image posts still exist as orphans. Step 2 would not work because there is no longer a parent post to redirect them to – it would produce a redirect loop and the page would time out. In such cases it would make sense to redirect orphan image posts to the site’s home page instead.

Step 1 – could be achieved by editing each image you have inserted in a post – the Link URL box should be empty if you want to avoid visitors clicking through to the image post. In our example below, pressing None will clear the text box – we would then press Update to update the image and press Update to publish the amended post:

WPimage

[WordPress remembers the state of the Link URL box so, if you choose None, all images in future new posts that you publish will be set to None automatically]

However, if you have hundreds of published posts with clickable images, it may not be realistic to edit and republish them all manually. In this case, leave the images as clickable but rely on Step 2 – if a visitor clicks an image, they will be redirected back to the same post i.e. stay on the main post regardless.

Steps 2 and 3 – can be achieved with the following code snippet – a new Function which should be inserted into Functions.php

/*
* Redirect Attachment Pages
*/
add_action( 'template_redirect', 'wpse27119_template_redirect' );
function wpse27119_template_redirect()
{
// Get out of here if not headed to an attachment page	
if( ! is_attachment() ) return;	

// Find the $post variable representing the page where we're heading
global $post;
if( empty( $post ) ) $post = get_queried_object();	

// Check if post has a parent	
if ($post->post_parent)
	{
	// Yes so find permalink of parent post
	$link = get_permalink( $post->post_parent );
	// Redirect attachment to the parent post
	wp_redirect( $link, '301' );
	exit(); // always call exit after wp_redirect
	}
else	
	{
	// No parent so just redirect to home page
	wp_redirect( home_url(), '301' );
	exit(); // always call exit after wp_redirect
	}
}

The code logic is simple – only takes effect for attachment pages, finds the name of the attachment page then checks if it has a parent. If it does, we do a permanent redirect to the parent else we redirect to the home page.

If you have never deleted a post you could remove the option of redirecting to the home page but it makes sense to leave it in to avoid a redirect loop if a post is ever deleted in future.

Notes On Code Snippet

The basis of this code is from Chris at Github but I have amended it to add a conditional check for orphan posts. Do check that the purpose is suitable for your site and amend if required – let me know in the comments if you have any suggestions.

Exceptions

1. You may not want to use this code if you have clickable links to images that are different to the ones in the main post e.g. links to larger size images. Likewise, if you have non-image attachment pages or are using links to images for use in galleries.

2. Some SEO plugins may offer redirect of image attachments e.g. Yoast’s SEO plugin has this as an option (not sure if/how it works with orphans).

3. If you don’t want any images to appear in Google’s image index you could disallow Googlebot-Image in robots.txt – see Google support for details. However, for most sites, image search can be a valuable source of visits and the above code snippet takes visitors to your more interesting parent post instead of just the standalone image.

1 Response to: "How To Redirect Image Attachment Pages In WordPress"

  1. Clint says:

    Hi, I’m actually using the Robots Meta plugin to have my attachments redirect, since they have been indexed in Google (I had also tried this with Yoast). But I now have a redirect loop and I just can’t seem to get them to properly redirect.

    Here’s an example – http://nonprofitelite.com/?attachment_id=420

    Any ideas?