Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggreagtion Enquires Welcome

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.

1
<?php 
2
/*
3
 Redirect a WordPress Image Page or Other Post Type to the Parent Post
4
 http://www.beliefmedia.com/redirect-wordpress-image-page
5
*/
6
 
7
8
  global $post;
9
    if ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent != 0) ) {
10
      wp_redirect(get_permalink($post->post_parent), 301); /* Redirect to post parent */
11
      exit;
12
    } elseif ( is_attachment() && isset($post->post_parent) && is_numeric($post->post_parent) && ($post->post_parent < 1) ) {
13
      wp_redirect(get_bloginfo('wpurl'), 302); /* Redirect to home page if not associated to any post/page */
14
      exit;
15
    }
16
  }
17
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.

Download


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.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment

READY TO HAVE A CHAT? CALL US ANYTIME ON 1300 235 433