I'm attempting to create a Twitter style stream, so basically just index, a tag archive, and maybe a page or two while prohibiting access to everything else. For instance, they try to access a single post page and are tossed back to home. Is this appropriate use of redirects and wp_redirect, and if so, would it be better to create a dummy file (single.php for instance) that only contains wp_redirect or just use conditionals at the top of my header.php? Or am I totally off course?
2 Answers
It seems this would just be a function of your theme. If you don't provide links to single posts, categories, etc., then as far as your visitors are concerned, those things effectively don't exist.
If you really want to prevent access to things, you can't redirect in a template as headers have already been sent. You could maybe add an action to pre_get_posts
, inspect the query, and redirect at that point.
Here's a simple example:
function wpse30196_pre_get_posts( $query ) {
if( $query->is_home || $query->is_tag ):
return;
else:
wp_redirect( home_url() );
exit;
endif;
}
add_action('pre_get_posts', 'wpse30196_pre_get_posts');
wp_redirect() does not exit automatically and should almost always be followed by exit.