7

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?

7
  • There are several WP plugins offering this kind of capability. You may do better asking about this at wordpress.stackexchange.com
    – O. Jones
    Mar 1, 2014 at 14:12
  • I've searched the WP repository and can't find any. Are you able to provide some links? Mar 1, 2014 at 14:15
  • "author slug" as a search on the WP plugin repository finds several.
    – O. Jones
    Mar 1, 2014 at 16:40
  • sorry, I should have said i can't find 'a working' plugin :) Mar 1, 2014 at 17:12
  • I did an medium in-depth search in [wp-hackers], core.trac.wordpress.org and WordPress Development. Also for some htaccess here at Stack Overflow. Couldn't find any working solution :/ . . . . . Right now, I can only think of creating a page/post for each author (where we can set the permalinks easily) or doing something with the hook template_redirect.
    – brasofilo
    Mar 9, 2014 at 0:02

6 Answers 6

4
+25

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;
}
12
  • I've removed my answer (using global $wp_rewrite) until able to fully debug it.
    – brasofilo
    Mar 5, 2014 at 18:18
  • you need a filter for WP_Rewrite. other ways, when processing url it will have 404 error (will treat it as a post). use author_rewrite_rules In fact I've managed today to make it working (your code with add_filter code from question). Try it.
    – Michal S
    Mar 5, 2014 at 18:54
  • Hi Michal S, thanks for this. It still doesn't seem to work for me? Not sure why! I think a combination of both your approach e.g. author_rewrite_rules and b__'s approach might work. @b__ are you able to repost your solution so we can all try to debug it? Thanks again to everyone Mar 6, 2014 at 10:28
  • where is your problem: author links on pages are wrong or when use them is 404?
    – Michal S
    Mar 6, 2014 at 10:56
  • With this approach, nothing happens. My author links are still site.com/author/user. @b__'s approach resulted in author links like site.com/user but broke the rest of the pages on my site. Mar 7, 2014 at 23:14
2

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.
1

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

This will go in your .htaccess

0

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]';
0

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.

-1

This Question is similar to this question...

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

Hope this will work...

1
  • Hi - thanks for your answer. Is the solution you linked to identical to the solution I said no longer works? Please see my question. Mar 4, 2014 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.