Last active
October 13, 2015 02:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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