Remove author prefix on WordPress

How to remove the author prefix on the WordPress website, I made a quick Google, but found only htaccess redirects that I do not want to resort to.

To clarify, I want to do this:

http://www.domain.com/author/cameron/

in that

http://www.domain.com/cameron/

I don’t want to use any redirects of any type, but the actual PHP code that I can use in the functions.php file, since I want all the links on the site that use the author’s material for automatic updates, links, and then redirection to a new one.

thanks

0
url-rewriting wordpress
source share
2 answers

Basically, you need to add WP rewrite rules to match the names of each of your users in the desired form. This is what WP No Category Base is for categories, so most of the code in my answer is adapted from this plugin.

The main part of the plugin is a function that connects to the author_rewrite_rules filter and replaces the author’s rewrite rules. This extracts all user names and adds a rewrite rule specifically for each user (feeds are not processed below, so if you need this, look at the source of the WP No Category database).

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

Another key part of the plugin is a function that intercepts the author_link filter and removes the author database from the returned URL.

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

See this entity: http://gist.github.com/564465

This does not handle redirection from the URLs of the old style authors, again, see the WP No Category Base source if you need to do this.

+8
source share

Be sure to replace this piece of code with no_author_base_rewrite_rules ():

 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]'; $rules = get_option( 'rewrite_rules' ); if ( ! isset( $rules['({$author->nicename})/?$'] ) ) { global $wp_rewrite; $wp_rewrite->flush_rules(); } } 

So Wordpress is updating the rewrite list. (otherwise some links may not work).

+1
source share

All Articles