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

3

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;
}
  • Thanks for the reply! I'm a noob. do i add this code to the functions.php only? or do i have to add something into the author.php file aswell – Missiona Aug 9 '18 at 4:29
  • WOW, you're the best!! I just added your code to my functions file and it seems to be working :) my agent role is getting the author.php file.. and my blog post by a contributor is getting the archive pages. Thank you so much! – Missiona Aug 9 '18 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.