Skip to content

Instantly share code, notes, and snippets.

@tarikcayir
Created November 8, 2016 14:35
Show Gist options
  • Save tarikcayir/3fcc7ae519f0b3f5d5664076dcf62cd9 to your computer and use it in GitHub Desktop.
Save tarikcayir/3fcc7ae519f0b3f5d5664076dcf62cd9 to your computer and use it in GitHub Desktop.
function momoth_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', 'momoth_redirect_attachment_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment