Heres a little trick on how to remove the WordPress author prefix from the author slug.
To clarify, this snippet will help you turn: ‘http://domain.com/author/peter/‘ into: ‘http://domain.com/peter/‘.

Since we want all links across the site that uses the WordPress author paradigm to auto-update without keeping the original links and then redirecting to a new one, We do not want to use any redirects of any kind but create an actual function in the WordPress functions.php file. We need to add WP rewrite rules to match the names of each of our users in the desired form.

  • The first part of markup hooks into the author_rewrite_rules filter and replaces the author rewrite rules. This retrieves all the usernames and adds a rewrite rule specifically for each user (the markup below won’t handle feeds, so look at the WP No Category Base source if you need that).
  • The second part of markup is a function which hooks into the author_link filter and removes the ‘author’ base from the returned URL.

Instructions
Add this code to your functions.php file.
*Remember to flush your rewrite rules after implementing this snippet, by going into “permalinks” within settings and then click “save changes”.

<?php
   add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
   function no_author_base_rewrite_rules($author_rewrite) { 
      global $wpdb;
      $author_rewrite = array();
      $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");    
      foreach($authors as $author) {
         $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
         $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
      }   
      return $author_rewrite;
   }
   add_filter('author_link', 'no_author_base', 1000, 2);
   function no_author_base($link, $author_id) {
      $link_base = trailingslashit(get_option('home'));
      $link = preg_replace("|^{$link_base}author/|", '', $link);
      return $link_base . $link;
   }
?>

To prevent collision with users creating usernames similar to pages or categories – you can either create a function or use a plugin that restricts usernames from being created – How to restrict usernames in WordPress.

Comments

  1. Thank you, this is beautiful 😉
    However, I am wondering what to do if you have a website dealing with language versions and domain-mapping. While the slug is being hidden in the original domain, the language mapped domain outputs “https://originaldomain.info/https:/mappeddomain.info/author/paul/”.

    Is there any way to change this and make the code work in this situation as well?
    This would be greatly appreciated.

    Best regards, Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

This could be the beginning of something great 🙌

Name
Hidden
Hidden

By submitting this form: You agree to the processing of the submitted data in accordance with WP Dynamics Privacy Policy.

Contact Information
You can also write us an email or contact us by telephone.
Telephone +45 71 74 71 21

Support For support inquiries, please refer to our support section.

Recent Articles

Faust.js – A Headless Framework for WordPress

Faust.js is a Headless WordPress Framework that provides a set of tools for building front-end applications based on WordPress ...

How to Load (Enqueue) and Handle Assets and Dependencies in WordPress

In WordPress, instead of adding assets such as styles and JavaScript to the header, you can and should use ...

An Introduction to Google Tag Manager Server-side Tagging

WordPress has come a long way since its launch in 2003 - from a blogging tool to a full-fledge ...