Created
June 10, 2016 14:14
-
-
Save wp-kitten/90f686e60993f5f6a2381aae0d7b6d21 to your computer and use it in GitHub Desktop.
[WordPress] Redirect certain pages to 404
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
/* | |
* 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