Skip to content

Instantly share code, notes, and snippets.

@wp-kitten
Created June 10, 2016 14:14
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 wp-kitten/90f686e60993f5f6a2381aae0d7b6d21 to your computer and use it in GitHub Desktop.
Save wp-kitten/90f686e60993f5f6a2381aae0d7b6d21 to your computer and use it in GitHub Desktop.
[WordPress] Redirect certain pages to 404
/*
* WP creates a lot of pages we don’t really need. First in the list of the unwanted is the attachment page.
* Each image you upload gets its very own personal web page.
* Also the archive by Author if you are a sole blogger is totally unneeded.
* travel diaries maybe).
*/
function wpkTemplateRedirect ()
{
global $wp_query, $post;
if ( is_attachment() ) {
$post_parent = $post->post_parent;
if ( $post_parent ) {
wp_redirect( get_permalink($post->post_parent), 301 );
exit;
}
$wp_query->set_404();
return;
}
if ( is_author() || is_date() ) {
$wp_query->set_404();
}
}
add_action( 'template_redirect', 'wpkTemplateRedirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment