Remove Author Prefix From Slug

Heres a little trick on how to remove the 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 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 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 user names 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.

    // The first part //
    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;
    }
    
    // The second part //
    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;
    }
    
    
    

    Go to wp-snippet.com [source]