Recently, one of our clients building their website on WordPress with us had a requirement — they wanted to have a URL base structure for their author pages that was different from the WordPress default. Instead of the default /author/[username]
structure, they wanted to use /authors/[username]
instead. Although there are plugins for this, we decided to implement it into the theme we were developing for the client to make the functionality as lightweight as possible.
Turns out, the implementation was pretty simple, as WordPress has built-in hooks for this. To update the author page URLs, we need to do 2 things:
1. Updating the author page rewrite rules
The first part of getting the new author URLs to work is to set up the rewrite rules for them. In WordPress, rewrite rules specify which URL structures should direct to which kinds of pages on the site.
a. Formulating the rewrite rules
To get WordPress to load the author pages when our desired URL structures are accessed, we need to describe our new URL structure to WordPress in regex, and map it to the appropriate queries in index.php
,:
URL Regex | Regex Description | Queried URL |
---|---|---|
authors/([^/]+)/?$ | Captures all URLs following the format /author/[username]/ . | index.php?author_name=$matches[1] |
authors/([^/]+)/page/([0-9]+)/?$ | Captures all URLs following the format /author/[username]/page/[number] . | index.php?author_name=$matches[1]&paged=$matches[2] |
All WordPress pages can be loaded by passing the appropriate query arguments to index.php
. In the query strings to index.php
above, we are passing specific arguments to ask WordPress to load all posts from a specific author.
Here’s another simpler example. On our site, this article’s ID in the site’s database is 9647. Hence, you can also load it using the URL: https://blog.terresquall.com/index.php?p=9647
. Try it!
Article continues after the advertisement:
b. Implementing the rewrite rules
To implement the rewrite rules we have formulated above to modify the URL structure of author pages in our WordPress installation, we will need to:
- Hook some code to the
author_rewrite_rules
filter, and; - Go to Settings > Permalink on the backend and click on Save Changes without changing any of the settings. This will refresh the new rewrite rules onto the WordPress installation.
Below is the code that we are hooking to the author_rewrite_rules
filter.
function my_author_rewrite_rules( $rewrite_rules ) { $rewrite_rules['authors/([^/]+)/?$'] = 'index.php?author_name=$matches[1]'; $rewrite_rules['authors/([^/]+)/page/([0-9]+)/?$'] = 'index.php?author_name=$matches[1]'; } add_filter( 'author_rewrite_rules', 'my_author_rewrite_rules' );
2. Overwriting the author permalink output
Upon performing the steps above, the new authors/[username]
URL structure should start working on your WordPress site. However, when WordPress generates links to your author on their articles, the generated links will still use the old URL structure.
This is because WordPress uses its native get_author_posts_url()
method to generate author URLs, and the method still recognises the old URL structure when generating author URLs.
To update this URL structure, we will need to add another filter to author_link
, like so:
function custom_author_permalink_structure( $permalink, $author_id ) { return home_url( '/authors/%author%' ); } add_filter( 'author_link', 'custom_author_permalink_structure', 10, 2 );
This will tell the get_author_posts_url()
function to generate author links using our new URL structure instead. The home_url()
method will append the site’s base URL onto the URL that we provide as an arguement, and the %author%
rewrite tag will be replaced with the author’s username.
3. Conclusion
Did this article help you with what you were looking for? If it is missing any information, we will really appreciate it if you can supplement us with it in the comments below.
Article continues after the advertisement: