Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 2 user roles. "photographer" and "subscriber"

I have set my author.php like this

<?php get_header(); 
    $thisauthor = get_userdata(intval($author));
    $author_user = $wp_query->get_queried_object();
        ?>
     <div id="main">
     <?php
   if (user_can($author_user, 'photographer')) { 
      get_template_part( 'author', 'photographer' ); }
   else if (user_can($author_user, 'subscriber')) { 
      get_template_part( 'author', 'subscriber' ); }
     ?>
   </div><!--/main-->
     <?php get_footer(); ?>

This is working great to show different profiles based on the user roles. However id like to change the url based on the roles.

Something like

http://xxx.xxx/subscriber/username/

http://xxx.xxx/photographer/username/

Any ideas on getting this done?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

To change all author URLs one would normally change the author base, however, since you want multiple author bases, we'll have to do things a bit differently.

There are two parts to this - The first part is getting WordPress to recognize incoming requests for your custom author pages and route them properly. To do this we'll add two new rewrite rules that take anything after a request for subscriber/ or photographer/ and pass that as an author_name to the main query. WordPress will process these as is_author requests, just like normal author pages.

function wpa_author_rewrite_rules(){

    add_rewrite_rule(
        'subscriber/([^/]+)/?$',
        'index.php?author_name=$matches[1]',
        'top'
    );

    add_rewrite_rule(
        'photographer/([^/]+)/?$',
        'index.php?author_name=$matches[1]',
        'top'
    );

}
add_action( 'init', 'wpa_author_rewrite_rules' );

Note that rewrite rules will need to be flushed once after adding this code so they will take effect.

The second part of this is for WordPress to generate the correct URLs when you use the API to output author URLs. Modifying WordPress output is typically done via a filter, as is the case here as well. We modify the output of functions that generate author URLs via the author_link filter. You'll recognize the user_can function in use here to check if the $author_id has the subscriber or photographer roles, returning custom URLs for those cases.

function wpa_author_link( $link, $author_id, $author_nicename ){

    if( user_can( $author_id, 'subscriber' ) ){

        return home_url( 'subscriber/' . $author_nicename . '/' );

    } elseif( user_can( $author_id, 'photographer' ) ) {

        return home_url( 'photographer/' . $author_nicename . '/' );

    }

    return $link;

}
add_filter( 'author_link', 'wpa_author_link', 10, 3 );
share|improve this answer
    
Nevermind i got it working. Didnt have the last "add_filter( 'author_link', 'wpa_author_link', 10, 3 );" in. –  Noob Theory May 8 '13 at 16:54
    
hmm, that shouldn't happen. if you look at that function in source, it calls get_author_posts_url, which has the author_link filter applied to it. do you have anything else, a plugin, that may be altering that output? –  Milo May 8 '13 at 17:03
    
I got it working. during copy/paste i had left out the last line add_filter( 'author_link', 'wpa_author_link', 10, 3 ); –  Noob Theory May 9 '13 at 1:50
    
Is there a way to pull the URL from the nickname? –  Noob Theory May 9 '13 at 10:42
    
anyone help with this? –  Noob Theory May 11 '13 at 8:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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