Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active October 13, 2015 02:29
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 birgire/e336ff2505094f3e0bbe to your computer and use it in GitHub Desktop.
Save birgire/e336ff2505094f3e0bbe to your computer and use it in GitHub Desktop.
WordPress: Redirect visits to the attachment pages to the parent page or the home page
/**
* Redirect attachment page visits to the parent page, else the home page.
*/
add_action( 'template_redirect', function()
{
if( ! is_attachment() )
return;
if( $parent_id = wp_get_post_parent_id() )
wp_safe_redirect( esc_url( get_permalink( $parent_id ) ) );
else
wp_safe_redirect( esc_url( home_url( '/' ) ) );
exit;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment