Enrich Your WordPress Knowledge Regularly
How to redirect the Post Author to a custom URL
I’ll share a quick tip about how to redirect the Post Author Archive page based on the condition in this short post.
If you are using the WPUF plugin, you will notice that while your users don’t have any posts and are visiting his frontend dashboard, there is no post found, like below.
Now once you click on the Users Display Name (like Subscriber User), you will show it will redirect you to a 404 template page like below:
So, now, if you want to redirect your user’s archive template based on the condition, then you can insert this below code on your theme/child-theme’s functions.php file, which will make the job easier.
/** Use this code on your theme functions.php file **/
add_action( 'template_redirect', 'redirect_author_archive' );
function redirect_author_archive() {
$id = get_query_var( 'author' );
$post_count = count_user_posts( $id );
$link = 'http://yoursitename.com/account/'; //Insert your link where you want to redirect.
if ( is_author() && $post_count<= 0 ) {
wp_redirect( $link, 302 );
exit;
}
}
Like in my example snippet, I want to redirect the users to the My Account page if they don’t publish any posts.
Well done!
Now, it will redirect the users to the associated link.