ReWrite the Slug of Default Authors Profile Page

In WordPress, we have the option to display the author’s profile. Ofcourse you need to have “author.php” page to make such page visible to user. You also may need to hyperlink it somewhere within the loop so that visitors can visit the author’s page and find some additional information.

By default, the URL for such page could be something like this:

https://www.test.com/author/username

However, it is possible to change the “author” slug and change it to anything you want. It can easily be achieved by using the WordPress WP-Rewrite class. After trying it out on my local server, I thought may be it’s time to share this tweak. So, here is the function.

<?php
function rewrite_author_slug() {
    global $wp_rewrite;
    $new_slug = 'writer';
    $wp_rewrite->author_base = $new_slug; }
add_action('init','rewrite_author_slug');
?>

Thing’s can’t get any simpler than this.

Explanation

What exactly I am doing here is pretty simple. On my function, I first initialized the global $wp_rewrite variable. WordPress suggests not to call WP_Rewrite directly, rather it requests to use $wp_rewrite object. Now, I declared $new_slug variable with my desired slug, in this case it is “writer”. On fifth line, I called the $author_base property of $wp_rewrite object to be equal to our $new_slug. Finally, I hooked up my function with an add_action that would be initialized (through init action) as soon WordPress finishes loading.

Note: Once you are done with adding this snippet on your site’s functions.php file, please make sure to flush your rewrite rules manually to get it working.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.