0

I'm trying to set it so that when you view somebody's profile page you see a different template based on their role. So, everybody gets a regular "profile" page but if your also a author you also get a special "editor" page as well.

Normally in Wordpress you get...

www.mysite.com/author/person1

but what I want is...

www.mysite.com/profile/person1
www.mysite.com/profile/person2
www.mysite.com/editor/person2

Below is what I've got so far in my functions.php file but the line that sets variable $curauth doesn't seem to work here. But it does work in my loop.php file???

function change_author_permalinks() {
    global $wp_rewrite;

    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
    var_dump($curauth);
    if ( !get_user_role( 'subscriber', $curauth->ID ) ) {
        $wp_rewrite->author_base = 'editor';
    } else {
        $wp_rewrite->author_base = 'profile';
    }

    $wp_rewrite->flush_rules();
}
add_action('init','change_author_permalinks');

1 Answer 1

1

i needed something similar to this so i added a rewrite rule to the same author.php file and there i added a redirect based on the user role, so to put this in your use case:

First redirect editor to your theme author.php file by adding this rewrite rule

function my_rewrite_rules_098( $wp_rewrite ) {
  $newrules = array();
  $new_rules['editor/(\d*)$'] = 'index.php?author_name=$matches[1]';
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules','my_rewrite_rules_098');

Next create a function to check the user role, say in your functions.php file:

function user_has_role( $roles_to_check = array(), $user_id ) {

  if( ! $roles_to_check ) return FALSE;
  if( ! $user_id ) return FALSE;

  $user = new WP_User( $user_id ); // $user->roles

  return in_array( $roles_to_check, $user->roles, FALSE );
}

then in your author.php theme file at the top add this:

if(isset($_GET['author_name'])) {
    $curauth = get_userdatabylogin($author_name);
}else{
    $curauth = get_userdata(intval($author));
}
//check user role  
$user_role_exists = user_has_role( array('Author'),$curauth->ID; );
if ($user_role_exists && isset(get_query_var('editor'))){
    //either rediret to a different editor.php template file 
    //or include it here something like:
    include('editor.php');
    exit;
}

Hope this helps

2
  • hmmm I'm still getting the same result in $curauth = bool(false). Should $author_name and $author be set as global variables?
    – EddyR
    Commented Feb 27, 2011 at 10:05
  • not in functions.php only on author.php
    – Bainternet
    Commented Feb 27, 2011 at 10:12

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.