404’s created /every new WP user getting their own author page

/*
 * Redirect unknown WP authors to me, Kevin! :D 
 * Created in response to every new WP user getting their own author page.
 * Just because a user is created doesn't mean they should have their own author page,
 * which ends up getting indexed by google thus creating a 404
 */
add_action( 'template_redirect', 'redirect_unknown_authors' );
function redirect_unknown_authors()
{
	/* 
	 * Check URL does not contain finishRedirecting flag but does contain /author/
	 */
	if( strpos($_SERVER['REQUEST_URI'], 'finishRedirecting=true') === false &&
		strpos(strtolower($_SERVER['REQUEST_URI']), '/author/') !== false ){
		
			$id = get_query_var( 'author' );
			$author_obj = get_user_by('id', $id);
			$post_count = count_user_posts( $id );
	
			if ( $post_count > 0 ) 
			{ 	
				// Queried author has posts, redirect user
				if ( wp_redirect( '/author/' . $author_obj->display_name . '/?finishRedirecting=true', 301 ) ) {				
				 exit;
				}
			} else {
				/* 
				 * If URL contains /author/ and the queried author has no posts,
				 * redirect to author, Kevin.
				 */
				if ( wp_redirect( '/author/Kevin/?finishRedirecting=true', 301 ) ) {
				 exit;
				}
			}
	} 
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x