Skip to content

Instantly share code, notes, and snippets.

@andcam
Last active November 7, 2018 17:46
Show Gist options
  • Save andcam/a8e717f662c788ec49ed3f518cb1a0a0 to your computer and use it in GitHub Desktop.
Save andcam/a8e717f662c788ec49ed3f518cb1a0a0 to your computer and use it in GitHub Desktop.
Wordpress - redirect all attachment pages to parent post or home page
// 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