After reading the following suggestions from Jan Fabry regarding URL rewrite and the author slug I took the challenge in combining both solution in one.
- Change author base slug for different roles
- Change the author slug from user name to nickname
Goal: The goal of this is to have a structure for the authors as followed:
http://domain.tld/employees/(department|group)/firstname-lastname
Current solution: In order to arrange this I have merged the solutions as mentioned in the referred answers as followed:
// NOTE: This need to be stored different and need to be attached to author/profile
// Will be added to user settings so this can be changed (only primary group will be stored)
$wpa04142013_author_groups = array( 'staff', 'admin', 'support', 'itnerds', 'etc' );
/*
* Init WordPress to add globals which can be used for the authors and levels of the authors.
*/
add_action( 'init', 'wpa04142013_init' );
function wpa04142013_init()
{
global $wp_rewrite;
$author_groups = $GLOBALS['wpa04142013_author_groups'];
// Define the tag and use it in the rewrite rule
add_rewrite_tag( '%author_group%', '(' . implode( '|', $author_groups ) . ')' );
$wp_rewrite->author_base="employees/%author_group%";
}
Above solution will make sure the author_base
is changed as needed based upon the goal defined in the beginning. After that the author_link
needs to be hooked in other to use the correct link to the author archive page.
add_filter( 'author_link', 'wpa04142013_author_link', 10, 3 );
function wpa04142013_author_link( $link, $author_id, $author_nicename )
{
//NOTE: This if/else needs to be changed --> either select case and use
// global defined groups based upon user property
if ( 1 == $author_id ) {
$author_group = 'staff';
} else {
$author_group = 'admin';
}
$link = str_replace( '%author_group%', $author_group, $link );
//Below solution added from other WordPress Answers suggestion
$author_nickname = get_user_meta( $author_id, 'nickname', true );
if ( $author_nickname ) {
$link = str_replace( $author_nicename, $author_nickname, $link );
}
return $link;
}
After implementing above the link to the author is working correctly, but the request and url rewrite is not working. Clicking the link of the author will result in a 404 page for the template.
If I am not mistaken I also need to modify the following section, but this is where it is going wrong.
/*
* Hook into 'request' to modify the author request.
* Change the way the lookup works (via nickname in stead of the slug)
*/
add_filter( 'request', 'wpa04142013_request' );
function wpa04142013_request( $query_vars )
{
if ( array_key_exists( 'author_name', $query_vars ) ) {
global $wpdb;
$author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
add_filter( 'author_rewrite_rules', 'wpa04142013_author_rewrite_rules' );
function wpa04142013_author_rewrite_rules( $author_rewrite_rules )
{
foreach ( $author_rewrite_rules as $pattern => $substitution ) {
if ( FALSE === strpos( $substitution, 'author_name' ) ) {
unset( $author_rewrite_rules[$pattern] );
}
}
return $author_rewrite_rules;
}
I have already gotten some help on the Dutch WordPress support forum, but I am still stuck in finding the right solution. Also looked into the rewrites with the Rewrite Analyzer, but could not find the author_group
. Hopefully you can help me pointing me in the right direction.