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.

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.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Edit: previous answer not working. Following solution does not provide sanitized nicknames, but should do the trick:

    $nn = urldecode($query_vars['author_name'] );
    $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $nn ) );
share|improve this answer
    
Based upon your suggestion I have changed the code and flushed the permalink structure in order to make sure the changes are being applied. This seems to work and I no longer receive an 404 message when opening the link to the author archive page. What I do not understand is why I need to unset the author rewrite rules with the function wpa04142013_author_rewrite_rules. –  Ruben Woudsma Apr 15 '13 at 14:43
add comment

I have ran some additional tests and came to the following conclusion. Besides the answer above it is required to flush the permalinks in order to apply the changes to the functions.php.

  1. Save changes to the functions.php
  2. Open Options --> Permalinks.

No need to save the changes, just visiting the page is enough to flush the permalinks. Furthermore it is must to verify the new rewrite rules with the Rewrite Analyzer, if the changes are not available there follow above steps to flush the permalinks.

Rewrite rules regarding author base and page slug with groups

share|improve this answer
add comment

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.