Aleš Sýkora / November 28, 2023 / 0 comments
Disable WordPress Autor archives
2 min read / Custom Code, WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: Don’t need to use Author archives in WordPress? Don’t forget that those pages are being generated automatically. So you need to set up redirect if you don’t need them. You can use following codes in functions.php or in custom mu-plugin, or my preffered way is using Advanced scripts. Redirect Author archives to homepage If you…
Don’t need to use Author archives in WordPress? Don’t forget that those pages are being generated automatically. So you need to set up redirect if you don’t need them. You can use following codes in functions.php or in custom mu-plugin, or my preffered way is using Advanced scripts.
Redirect Author archives to homepage
If you want user to be redirected to the Homepage, use this function. Put the code to your Advanced scripts or code snippets plugin.
<?php
add_action('template_redirect', 'great_tit_disable_author_archives'); //add action
function great_tit_disable_author_archives() { //create function
if (is_author()){ //check if page is author archive page
$target = get_option('siteurl'); //get site url
$redirect = '301'; //set up redirect status 301 = permanent redirect
wp_redirect($target, $redirect);
die();
}
}
You can change the $target to specific URL if you need redirect to specific URL:
$target = 'https://www.great-tit.com/target/'
Set 404 not found to author archives
This code will set author archives to respond with not found 404 error code.
add_action('template_redirect', 'great_tit_disable_author_archives_404');
function great_tit_disable_author_archives_404() {
global $wp_query;
if ( is_author() ) {
$wp_query->set_404();
status_header(404);
}
}
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻