By default, WordPress creates author archive pages for each user who has published content on your site. These archive pages can be accessed by visiting a URL in the format example.com/author/username
. However, in some cases, you may want to disable the author archive pages to prevent duplicate content or to simplify your site structure. Here’s an example code snippet to disable author pages in WordPress:
function wpsnippets_disable_author_pages() {
// Redirect author archive pages to the home page
global $wp_query;
if ( is_author() ) {
$wp_query->set_404();
status_header( 404 );
wp_safe_redirect( home_url(), 301 );
exit;
}
}
add_action( 'template_redirect', 'wpsnippets_disable_author_pages' );
In this code snippet, we define a function wpsnippets_disable_author_pages()
that redirects any requests for author archive pages to the home page. We do this by checking if the current page is an author archive page using the is_author()
function. If it is, we set the HTTP response code to 404 using status_header()
, perform a 301 (Moved Permanently) redirect to the home page using wp_safe_redirect()
, and exit the script using exit
.
This code snippet will effectively disable the WordPress author archive pages, since any requests for author archive pages will be redirected to the home page. Note that this code only applies to requests for author archive pages and does not affect other aspects of your site’s functionality.
If you’re only planning on redirecting the author pages temporarily, it’s better to swap the 301
(Moved Permanently) redirect for a 302
(Moved Temporarily) redirect.