Question

  1. I have a author.php template that styles a user profile (custom role: agents,etc)

  2. I have a blog and Editors & Contributors can post articles.

Problem

When a viewer on the blog clicks an author link to view the Author Archive page, the page that is loaded is the Author.php template. This template shows agent profile details and no articles. this author.php template has nothing to do with Editors & Contributors.

Solution?

How can i exclude Editor & Contributor from loading this author.php file? That way when a user visits the blog post authors link, it takes them to Author Archive page insted of the agent author.php custom template

Hope this is clear enough, Thanks for the help!

Was it helpful?

Solution

The template_include hook allows you to change which template file is going to be used.

In it, you can conditionally check if it's an author page requested, then use get_query_var() to get the requested author. You then check the authors role and see if it's what you don't want, and use a different template if so.

Below is untested, but something like:

add_filter( 'template_include', 'myplugin_author_redirect', 99 );

function myplugin_author_redirect( $template ) {

    if ( is_author() ) { // When any Author page is being displayed

        // get the user page being requested
        $user = get_user_by('slug', get_query_var('author_name'));
        $user_role = array_shift($user->roles);

        // user roles we want to redirect away
        $exempt = array('editor', 'contributor');
        if ( in_array($user_role,$exempt,true) ) {
            // wtv file you want to load when an editors or contributor author page is requested
            return locate_template( array( 'archive.php' ) );
        }
    }
    return $template;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top