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?
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/
orphotographer/
and pass that as anauthor_name
to the main query. WordPress will process these asis_author
requests, just like normal author pages.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 theuser_can
function in use here to check if the$author_id
has thesubscriber
orphotographer
roles, returning custom URLs for those cases.