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?

Read More

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' );

Related posts

Leave a Reply

3 comments

  1. 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.

  2. Here’s a possible approach, to be implemented in author.php:

    <?php 
        global $wp_query;
        $roles = $wp_query->queried_object->roles;
        if( ! $roles[0] == 'author' ) {
            echo '<p>Nothing here!</p>';
        } else { 
            // show author archive
        }
    ?>
    

    If you do not wish to edit templates, take a look at the template_redirect function

  3. edit htaccess try this:

    <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond %{HTTP_USER_AGENT} !Google [NC]
     RewriteRule ^author/?([_0-9a-z-]+)?/?$ http://misite.com [R=302,NC,L]
    </IfModule>