How to remove author base in WordPress

2020-03-02 06:06发布

Standard author links in WordPress look like: example.com/author/johnsmith

I'd like to remove the author/ part of the URL so the username is in the root. For example: example.com/johnsmith

I control page creation on my site so there will be no chance of a conflict in page and author name.

So far I've tried the following solution from WP Snippet but this no longer seems to work:

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;
}

Do anyone know if there is a working solution to this?

6条回答
小情绪 Triste *
2楼-- · 2020-03-02 06:26

It seems we have to refresh permalinks structure, but each time someone registered a new rule.

So try to flush it automatically with this :

$wp_rewrite->flush_rules();

In your functions.php file.

I had the same problem but with this it seems to be ok. But there's still a problem of conflict with page & post name.

查看更多
何必那么认真
3楼-- · 2020-03-02 06:33

Try: RedirectMatch 301 ^/author/(.+)$ http://www.yourblog.com/$1

This will go in your .htaccess

查看更多
狗以群分
4楼-- · 2020-03-02 06:38

Your code appears fine. Manually flush your permalink structure to reflect these changes.

1)  Settings -> Permalinks -> choose default -> Save
2)  Revert the settings to original.
查看更多
▲ chillily
5楼-- · 2020-03-02 06:39

I've tested this combined solution but wasn't working before regenerating of permalinks. You can do it, as brasfolio described : simply clicking save on permalink page in dashboard.

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;
}

if( !is_admin() ) {
add_action('init', 'author_rewrite_so_22115103');
}

function author_rewrite_so_22115103() {
   global $wp_rewrite; 
   if( 'author' == $wp_rewrite->author_base ) $wp_rewrite->author_base = null;
}
查看更多
▲ chillily
6楼-- · 2020-03-02 06:41

This Question is similar to this question...

You can do something like This. This is nearly what you wanted..

Hope this will work...

查看更多
啃猪蹄的小仙女
7楼-- · 2020-03-02 06:45

Also add this near the other similar two, in order to redirect the feeds as well:

$author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
查看更多
登录 后发表回答