WordPress by default creates an author profile for every user publishing content on your website. This is a great way to organize articles and other types of data posted by separate users in a cleaner, better manner. But, in some cases, you may need to make changes to the location where the posts and author information is listed. Everyone may not like seeing it under the /author/ section. In that case, this code snippet will be helpful. You can change the default “author” keyword in your WordPress author page URL to whatever you want using the following code snippet.
For example, you can change site.com/author/john-doe/
to site.com/profile/john-doe/
.
Usage
- If you have a separate login/registration system using plugins such as Ultimate Member that provide separate author profile sections. So, in order to avoid multiple sections for the same author, you might wanna remove the default author section.
- Just to move the current default URL to something better that matches the workflow and features of your website.
- To shorten the URLs with something such as a single letter like /u/ for some reason.
Code
// Change Author Slug
add_action('init', 'wpt_change_author_base');
function wpt_change_author_base() {
global $wp_rewrite;
$author_slug = 'profile'; // replace "profile" with anything
$wp_rewrite->author_base = $author_slug;
}
Explanation
- A comment to identify the function from other code blocks.
- Using the
add_action
function, we hook our newly created functionwpt_change_author_base
to theinit
to load it right after WordPress has finished loading. (You can also add this line at the very bottom after defining the function). - We define the function
wpt_change_author_base
. - Declaring the global variable
$wp_rewrite
(the instance of WP_Rewrite class) so that we can access theauthor_base
from it. - Creating a variable
$author_slug
and assigning it a value we want to use for our author slug instead of the default. - Finally, we assign the value of our
$author_slug
variable to theauthor_base
. - And close the function.