Wordpress - How to remove the author pages?
The above answer is good, but if redirecting to home page, it should specify a 301 status and exit after.
add_action('template_redirect', 'my_custom_disable_author_page');
function my_custom_disable_author_page() {
global $wp_query;
if ( is_author() ) {
// Redirect to homepage, set status to 301 permenant redirect.
// Function defaults to 302 temporary redirect.
wp_redirect(get_option('home'), 301);
exit;
}
}
wp_redirect() documentation https://developer.wordpress.org/reference/functions/wp_redirect/
You can also add the redirect to the author template directly. In your WordPress theme, edit the author.php file to redirect users to your homepage. If your theme doesn't have a template for author pages, create a file named author.php.
author.php: (Using php header function)
<?php
//Redirect author pages to the homepage
header("HTTP/1.1 301 Moved Permanently");
header("Location: /");
die(); // avoid further PHP processing
//That's all folks
The die()
part is to avoid that anyone using a client which does NOT follow the redirect header sees the content of the page since WP would continue building the original author page and send its response to the client requesting it.
UPDATE: WordPress has a couple of built in functions to handle redirects: wp_redirect() and wp_safe_redirect(). wp_redirect() accepts a string as the redirect location and an integer as the redirect type (302 is the default). wp_safe_redirect() is the same as wp_redirect() except that it makes sure that the redirect location is found in a list of allowed hosts.
author.php: (Using WordPress wp_safe_redirect function)
<?php
//Redirect author pages to the homepage with WordPress redirect function
wp_safe_redirect( get_home_url(), 301 );
exit;
//That's all folks
More information
- WordPress template hierarchy: https://developer.wordpress.org/themes/basics/template-hierarchy/
- PHP header function: https://www.php.net/manual/en/function.header.php
- WordPress wp_safe_redirect function: https://codex.wordpress.org/Function_Reference/wp_safe_redirect
You can disable the access to author pages by adding this snippet to functions.php:
// Disable access to author page
add_action('template_redirect', 'my_custom_disable_author_page');
function my_custom_disable_author_page() {
global $wp_query;
if ( is_author() ) {
$wp_query->set_404();
status_header(404);
// Redirect to homepage
// wp_redirect(get_option('home'));
}
}