author.php shows profiles of all users not authors only

I’ve got an issue with author’s template. It works great and displays authors profiles as expected, but the issue is that you can see non-authors profiles also (contributors / subscribers, basically all registered users, including and admin) simply by substituting username in URL (www.mysite.com/author/subscribername).

Is there any simple way to fix it and show ‘authors’ profiles only and return ‘page not found’ for others?

In advance of your support, thank you.

Update:
Solution I’ve applied and will use (unless someone will advise how to amend the initial query). This returns proper ‘page not found’ page, including ‘page not found’ page title:

function tb_author_role_template( $templates="" )
{
    global $wp_query;

    $author = get_queried_object();
    $role = $author->roles[0];

    if($role == 'author') 
    {
        $templates=locate_template(array("author.php",$templates),false);
    } else { // error if not author
        header( 'HTTP/1.0 404 Not Found' );
        $templates = locate_template(array("404.php",$templates),false);
        $wp_query->is_404 = true;
    }
    return $templates;
}
add_filter( 'author_template', 'tb_author_role_template' );

3 Answers
3

You’re going to want to use the author_template filter to process this. Just to clarify, the filter’s “author” means all users, it appears. I’m not sure why. This will be called while the backend is deciding which template page to use when showing a user’s profile on your site. You need to add an author-editor.php file and then filter to it if the user matches your criteria.

function author_role_template( $templates="" )
{
    $author = get_queried_object();
    $role=$author->roles[0];

    if($role == 'author') {
        $templates=locate_template(array("author-author.php",$templates),false);
    } else { // error if not author
        $templates=locate_template(array("404.php",$templates),false);
    }
    return $templates;
}
add_filter( 'author_template', 'author_role_template' );

Source: Codex — Template Hierarchy

NOTE: You’ll need to verify that roll returns true for this. I’m not 100% on it the comparison of the roll to the string.

Leave a Comment