Redirect a WordPress Image Page to the Parent Post

If you upload an image via the WordPress image library, of you have it imported in your code to a post or page, that image will be available in a standalone page that displays the image in full. They’re not necessarily any good for your SEO, and they arguably add very little to the user experience… in fact, in my opinion, they detract from it because of lack of content.

In my case, I recently build a little site that had some wallpaper images that were automatically uploaded to the media gallery (thus creating their own image page) but I wanted to track each full scale image download (via a URL rewrite) to get an idea of what was popular… and each post having its own page negated this feature to an extent.

The solution was to provide a small function that would redirect image/attachment pages to their parent page or, if the image didn’t have a post parent, to the website home page.

The WordPress Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.


Download the function here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *