Skip to content

Instantly share code, notes, and snippets.

@norico
Last active February 26, 2019 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norico/41e408b0c12ddfb4d5465fbc0f388009 to your computer and use it in GitHub Desktop.
Save norico/41e408b0c12ddfb4d5465fbc0f388009 to your computer and use it in GitHub Desktop.
<?php
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;
}
add_action( 'template_redirect', 'wpsites_attachment_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment