overview of the rewrite rules that have been created with this code snippet.
overview of the rewrite rules that have been created with this code snippet.

Recently I encountered the challenge in order to change the url and link to the author within a WordPress installation and theme. In order to get additional information regarding the solution I posted a question on StackExchange and on the Dutch WordPress forum.

Below you will find the code and solution to modify the author base slug and add group (or category) information to the author slug (URL) in combination with usage of the author nice name.

How to modify the author base slug with group information and nicename?

So what am I trying to achieve and what is the challenges I encountered. The main goal I want to achieve is the following:

[code language=”php”]http://domain.tld/employees/(department|group)/firstname-lastname[/code]

Thus create a link to the author page that will look like this:

[code language=”php”]https://rubenwoudsma.nl/onze-mensen/management/ruben-woudsma[/code]

If you would like to achieve the same then you need to make several changes to the WordPress structure. Below an overview of the steps:

  1. Define the groups you wish to use to assign the authors to;
  2. Change the base author slug (which also uses the groups);
  3. Modify the author link so it takes the ‘group’ and ‘nice_name’ into account;
  4. Modify/hook into the request so query/lookup is via nicename;
  5. Modify/hook the rewrite rules for the author slug.

This can easily be done by adding the following code to the functions.php or add this code within a specific file especially to handle all these additional code:

1. Define the groups you wish to use

The first step is to define the groups you wish like to use in the website for the authors. In the code below I set up the following groups:

[code language=”php”]// 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)
$rwsnippet_author_groups = array( ‘staff’, ‘admin’, ‘support’, ‘itnerds’, ‘etc’ );[/code]

As mentioned in the comment this set of groups can best be done via a global variable of constant value, so it can also be assigned to a user profile. This can be done via the earlier published code snippet.

2. Change the base author slug

In order to make sure the new base slug for the authors is being set you need to add the following code to the functions.php:

[code language=”php”]/*
* Init WordPress to add globals which can be used for the authors and levels of the authors.
*/
add_action( ‘init’, ‘rwsnippet_init’ );
function rwsnippet_init()
{
global $wp_rewrite;
$author_groups = $GLOBALS[‘rwsnippet_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%’;
}[/code]

3. Modify the author link

You also need to change the author link as this was one of the main goals mentioned above. The information in the URL of the website should contain the new base slug and group information as well as the nicename of the author. The following code will handle this part:

[code language=”php”]add_filter( ‘author_link’, ‘rwsnippet_author_link’, 10, 3 );
function rwsnippet_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;
}[/code]

As mentioned in the comment line the handling of the groups needs to be made different. In this example it has been done hard coded, but this should be set up flexible via a global setting or via theme options.

4. Modify/hook into the request being made based upon the adjusted author link

As the author link has been changed the request that is being sent to the application also needs to be changed. The code snippet below will handle this part:

[code language=”php”]/*
* Hook into ‘request’ to modify the author request.
* Change the way the lookup works (via nickname in stead of the slug)
*/
add_filter( ‘request’, ‘rwsnippet_request’ );
function rwsnippet_request( $query_vars )
{
if ( array_key_exists( ‘author_name’, $query_vars ) ) {
global $wpdb;
$nn = urldecode($query_vars[‘author_name’] ); //sanitize author name
$author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key=’nickname’ AND meta_value = %s", $nn ) );
if ( $author_id ) {
$query_vars[‘author’] = $author_id;
unset( $query_vars[‘author_name’] );
}
}
return $query_vars;
}

add_filter( ‘author_rewrite_rules’, ‘rwsnippet_author_rewrite_rules’ );
function rwsnippet_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;
}[/code]

How to verify the author slug modification?

Once you have done added the above code to the functions.php of the custom theme you need to flush the permalink structure. This is needed in order to make sure the above adjustments of the code snippets have been taken into account. This can be done by navigating to: Options –> Permalinks. Only opening this option page will flush the permalink. Furthermore you can check the result with the Rewrite Analyzer plugin.

Other solutions to modify the author link

If you only need to change the author archive link, then there are other solutions. Justin Tadlock wrote a nice article about this.

WordPress: Modify author base slug to use groups and nicename
Getagd op:                         

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *