0

The site I'm currently working on includes profile pages for each author plus a method to search for posts by a specific author. So we would have a page at /author/author-name/ that only displays a list of posts by that author and a page at something like /author/author-name/profile/ that shows extended user info but no posts.

By default Wordpress includes an author.php template that shows some basic user info and recent posts by the author. This is found at /author/author-name/. When looking for recommended ways to build a user profile, everything I have found recommends altering this file and using the same URL, however we would like to use both.

The only way I can think of achieving this would be to allow the page to take a parameter so that /author/author-name/?profile=1, rewrite it in .htaccess so that it appears as /author/author-name/profile/. Then alter author.php to check for this parameter and use a separate template. I would like to avoid methods such as this and stay on the Wordpress rails as much as possible.

Are there any plugins or other Wordpress approved methods I could use to solve this problem? (We're running 3.8 by the way).

2
  • WordPress features a Rewrite API that allows you to create endpoints, etc, without needing to customize your .htaccess file.
    – Pat J
    Commented Jan 21, 2014 at 21:54
  • If you got an answer, please don't add it to the question. Add it as answer. Brings upvotes and using Q/A to share knowledge and answer your own questions is fully ok.
    – kaiser
    Commented Jan 21, 2014 at 23:21

2 Answers 2

0

I would use add_rewrite_endpoint() with EP_AUTHORS target. That should take care of URLs and query var to check for [relatively :)] cleanly.

4
  • And use a GET parameter as I described to show different content?
    – Godwin
    Commented Jan 21, 2014 at 21:56
  • @Godwin read the linked docs please, endpoint will provide a query var to check for.
    – Rarst
    Commented Jan 21, 2014 at 21:59
  • Thanks, took a while to completely understand. I'm going to update my question to make it easier for others that are not familiar with add_rewrite_endpoint.
    – Godwin
    Commented Jan 21, 2014 at 22:50
  • @Godwin sorry if answer was too short, it was late night for me :) Could you please move your solution to an answer? That is more in line with site's mechanics than adding it as part of question.
    – Rarst
    Commented Jan 22, 2014 at 9:17
0

To expand on Rarst's answer, in case you're not familiar with add_rewrite_endpoint, here's what I came up with.

Create a template file named author-profile.php. In your functions.php file add the following:

add_action('init', 'theme_add_author_profile_endpoint');
add_action('template_redirect', 'theme_author_profile_template_redirect');

function theme_add_author_archive_endpoint() {
    add_rewrite_endpoint('profile', EP_AUTHORS);
}

function theme_author_archive_template_redirect() {
    global $wp_query;
    if (isset($wp_query->query_vars['profile']) && is_author()) {
        include dirname( __FILE__ ).'/author-profile.php';
        exit;
    }
}

This should allow /author/author-name/ to be directed to the default author.php template while /author/author-name/profile/ will be directed to author-profile.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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