Last active
November 7, 2018 17:46
-
-
Save andcam/a8e717f662c788ec49ed3f518cb1a0a0 to your computer and use it in GitHub Desktop.
Wordpress - redirect all attachment pages to parent post or home page
This file contains 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 pages to home | |
function ljxdm_redirect_attachment_pages() { | |
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', 'ljxdm_redirect_attachment_pages' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment