[SmartCrawl Pro] Smartcrawl – Redirect attachments partially works

We have been having an issue with Attachments that are no longer attached to a specific post. When we have Redirect Attachments checked in smartcrawl, it works for current attached images. However we have some posts that no longer exist, but the attachment page (for whatever reason) Still exists.

I found a solution that hopefully won't conflict with the smartcrawl plugin, there are actually two ways to do it.

The first way:

Edit your Image.php and replace the content with this

<?php

global $post;

if ( $post && $post->post_parent ) {

wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );

exit;

} else {

wp_redirect( esc_url( home_url( '/' ) ), 301 );

exit;

}

or you can edit the functions.php file and put this in:

function myprefix_redirect_attachment_page() {

if ( is_attachment() ) {

global $post;

if ( $post && $post->post_parent ) {

wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );

exit;

} else {

wp_redirect( esc_url( home_url( '/' ) ), 301 );

exit;

}

}

}

add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );

What this does is makes it so if your attachment has a parent post, it will redirect to the parent post (like smartcrawl does) But it handles the attachments that do not have a parent post. So if there is no parent post it redirects to the homepage.

I'm not quite sure if this is a feature request or a bug so i put it in as a ticket. If it's a feature request this needs to be put in as an option to enable for all attachments. Please let me know what you think.

  • Ash
    • WordPress Hacker

    Hello forthgear

    Thank you for your explanation and the code suggestion. Actually, this is not a bug, but SC is not designed to redirect the attachment page to elsewhere when it is not attached to any page.

    So, I am marking this as a feature request, let’s see how other members think about this. I am also sending a note to the smartcrawl team as well.

    Have a nice day!

    Cheers,

    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.