Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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