Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active August 17, 2017 09:05
Show Gist options
  • Save braddalton/eea4f721388c6b38248c to your computer and use it in GitHub Desktop.
Save braddalton/eea4f721388c6b38248c to your computer and use it in GitHub Desktop.
add_action( 'template_redirect', 'wpsites_attachment_redirect' );
function wpsites_attachment_redirect(){
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 );
exit();
wp_reset_postdata();
endif;
}
@89gsc
Copy link

89gsc commented Aug 17, 2017

Line 7 is unreachable because of the exit(); Also is there any need to reset the post data in this instance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment