Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

How to handle image attachments?

         

Gemini23

3:08 pm on Mar 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is the best way to handle the plethera of image attachments from posts that create unwanted urls.. and potential for Panda problems on a wordpress installation.

The 'norm' is obviously index/follow

so..

1. NoIndex/Follow
2. NoIndex/No Follow
3. 301 redirect to parent post
4. Canonical to parent post
5. Combination of the above or anything different?

any further information or plugins to achieve the desired outcome appreciated.

Nutter

8:00 pm on Mar 19, 2014 (gmt 0)

10+ Year Member



The Yoast SEO plugin has an option to automatically redirect the attachment post back to its parent. Probably other plugins that will do this as well if Yoast is a bit too heavy for your site.

lorax

1:07 am on Mar 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Saw this somewhere and had it saved. I have NOT tested this so be cautious.

If you have a child theme (or can create one) copy the image.php file to the child theme directory. Wipe it clean and put this code in. Save and upload.

If you don't have a child them, rename your original image.php file to something else for safekeeping then create a new version with this code.

It should redirect anyone trying to look at an image attachment page back to the original post. IF not attached to a post, then it should send them to the home page.


<?php
global $post;
$post_parent = $post->post_parent;
if ( $post_parent ){
wp_redirect(get_permalink());
} else {
$home_url = home_url();
wp_redirect( $home_url);
}
?>

Gemini23

12:02 pm on Apr 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have tried the attachment redirect plugin on one website and it works fine BUT... on another website it redirects everything to the homepage. I have tried the above code and it doesn't seem to do anything.

I have aioseo and not yoast

Nutter

3:02 pm on Apr 6, 2014 (gmt 0)

10+ Year Member



Shouldn't it be wp_redirect(get_permalink($post_parent)) instead?

lorax

4:08 pm on Apr 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@Gemini23 - sounds like you may have a redirect somewhere else?

@Nutter - The way I read that code it's your choice. You can redirect to the parent post as you suggest or send them wherever you'd like.

Gemini23

5:41 pm on Apr 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Lorax... I am not sure what else can be conflicting wit it...

I activate the attachment redirect plugin and it redirects every attachment to the homepage and if I deactivate it then it shows the attachment url... #head-scratching

Nutter

9:03 pm on Apr 6, 2014 (gmt 0)

10+ Year Member



@lorax - That's what I'm asking. In the snippet it has wp_redirect(get_permalink()). Without passing a post ID, won't that just redirect back to the same page?

lorax

1:36 pm on Apr 7, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@Nutter - Oh! Yes, I believe you're right.

wp_redirect(get_permalink( $post_id ));

$post_parent was probably a function earlier on in the example that I didn't snip and save.

Planet13

11:16 pm on Apr 7, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could someone explain to me exactly what the issue is here? I am kind of lost here.

Are you saying that if a post has an image, there will be a unique URL created for that image?

Gemini23

11:30 pm on Apr 7, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Plantet13 - yes that is what happens by default with Wordpress... and seems to be quite a long-standing issue...