How to Remove the WordPress Author Prefix from Slug
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”.
// 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; }
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.
This does not work. Is it out of date? If so any new technique??
Are you sure you can’t make it work?
I just used it 3-4 weeks ago, and it worked – could it be you have a conflict in your functions-/plugins-/htaccess?
Wahey, in case anyone needs it I found a plugin that is based way round this. Try out the “WP htaccess Control” plugin. It has the option to switch off the slug base for the author URL. You just save the standard settings with nothing in the field for author slug. It even states that’s the intention.
http://wordpress.org/extend/plugins/wp-htaccess-control/screenshots/
Thanks, Man. Your trick is the only that worked for me. And I’ve searched across the internet. I hope Google puts this in the first results. Congrats!
Thanks!!! You saved my night :)
I Used this to change the author base slug in WP 3.7 and it worked like a charm! Thanks very much :)
Does this work with WP 4.1?
I get this : Fatal error:
Call to undefined function add_filter()
in “/home/stark/domains/stark.idl.pl/public_html/wp-includes/functions.php” on line 4818Can anyone help?
It works, the site works ok, but i can’t acces the adminnistration side of WP…
It does work, but also broke the 301 redirections. The “domain.com/?author=1” doesn’t redirect to “domain.com/author_name” anymore.
How can feeds be redirected as well? I tried adding:
but it doesn’t seem to be working
I want to remove the profile slug from the url. here is the example : http://www.example.com/profile/premtiwari
One needs to replace “get_option(‘home’)” with “get_home_url()”, otherwise it will produce non valid urls in case if the home URL is saved with http protocol but the site is on a https (which is quite often a case).
It worked well, save hours of work, many thanks
This had been a great pain for last few days. thanks for this.
Whoever you are, you are great.
Hi, yes it works, but you have to flush the rewrite rules “every time” there is a new user… Do you know a way to do that? Or an alternative solution?
Thank you ;)