RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.74% (6.40%*) • Investment PI: 5.58% (6.62%*)

Redirect a WordPress Image Page or Other Post Type to the Parent Post

Redirect a WordPress Image Page or Other Post Type 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 as a standalone page that displays the image in full. While these pages might have their place in the world of photography when a theme properly supports attachments (and most don't), the page is little redundant... and isn't necessarily advantageous for your SEO. In my opinion, the attachment pages detracts from the average user experience because the image is set on a page without the relevant context.

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. Each image having its own page negated this feature.

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. It's a simple DIY SEO technique that can be easily accomplished in under 1 minute. Adding this very basic feature to a website is part of the much broader SEO packages that we make available to clients.

Shortcode 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. You may optionally download and install our plugin from the bottom of of the page.

 Redirect a WordPress Image Page or Other Post Type to the Parent Post
  global $post;
    if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) {
      wp_redirect(get_permalink($post->post_parent), 301); /* Redirect to post parent */
    } elseif ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent < 1) ) {
      wp_redirect(get_bloginfo('wpurl'), 302); /* Redirect to home page if not associated to any post/page */
add_action('template_redirect', 'beliefmedia_redirect_image_page', 1);

The same style of redirect can be applied so any post type can be redirected to any other.


Title: Redirect WP Image Attachment Page to Parent Post (WP Plugin)
Description: Redirects image attachment pages to their parent post or page.
  Download • Version 0.2, 1.5K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (531.0B)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?


Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment