wordpress, water, logo

WordPress started off as a blogging platform, which meant that every registered user was potentially an article author. Consequently, the permalink for a specific user’s archive was always /author/{username}. Even now that WordPress is no longer considered just a blogging platform but a fully-fledged CMS, that URL structure hasn’t changed. Moreover, it is not even possible to change the author base through the admin interface. In this article we will discuss an efficient way to change the author permalink structure.

These are my users

For the purpose of our simple example, we will assume that the desired URL structure is a generic /user/{username} one. The first order of business is to filter the author link creation:

function tutorial_author_link_filter($l,$uid,$name) {
  $url=sprintf('%s/%s/%s/',home_url(),'user',$name);
	return $url;
}

add_filter('author_link','tutorial_author_link_filter',10,3);
PHP

In a real life situation, we would have to flush_rewrite_rules() on plugin activation, but for the purposes of our example, it is sufficient to navigate to Settings > Permalinks and just push the save button.

From this point on, every “author” link WordPress creates, either on the front-end or the admin interface, will have the structure we mentioned above. However, those links don’t work yet. That’s because, while we defined their structure, WordPress still doesn’t know what to do with it.

And those are my rules

There is a set of rules for associating author URLs to their corresponding content. These rules come in the form of an associative array, with regular expressions as keys and internal URL templates as values:

[author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$]=>index.php?author_name=$matches[1]&feed=$matches[2]

[author/([^/]+)/(feed|rdf|rss|rss2|atom)/?$]=>index.php?author_name=$matches[1]&feed=$matches[2]

[author/([^/]+)/embed/?$]=>index.php?author_name=$matches[1]&embed=true

[author/([^/]+)/page/?([0-9]{1,})/?$]=>index.php?author_name=$matches[1]&paged=$matches[2]

[author/([^/]+)/?$] => index.php?author_name=$matches[1]

This array essentially tells WordPress which URL part should be translated to which internal query parameter. So the only thing we have to do is replace the author base with a user one using the author_rewrite_rules filter:


function tutorial_author_rewrites_filter($author_rewrites) {
	$rewrites=[];
	foreach($author_rewrites as $pattern=>$template) {
	  $key=str_replace('author','user',$pattern);
	  $rewrites[$key]=$template;
	}
	return $rewrites;
}

add_filter('author_rewrite_rules','tutorial_author_rewrites_filter');
PHP

After that, our links will lead to the correct content. The last thing to do is edit our theme’s author template to include the information we want to display in our users’ public profile. But that’s beyond the scope of this topic.