Wordpress Author Permalinks -
i know how change base of author permalinks, on site, refer users not username number based on user id, user number 5 wrote post, rather johndoe123 wrote post.
the problem comes when go users archives , instead of seeing example.com/authors/5/ see example.com/authors/johndoe123/ .
how change permalinks can pull author archives using following structure? :
[wordpress_site_url]/authors/[user_id]/
this can done adding new rewrite rules each user in same way when changing or removing author base. so, adapting code previous answer, add rewrite rules this:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules'); function my_author_url_with_id_rewrite_rules($author_rewrite) { global $wpdb; $author_rewrite = array(); $authors = $wpdb->get_results("select id, user_nicename nicename {$wpdb->users}"); foreach ($authors $author) { $author_rewrite["authors/{$author->id}/page/?([0-9]+)/?$"] = 'index.php?author_name=' . $author->nicename . '&paged=$matches[1]'; $author_rewrite["authors/{$author->id}/?$"] = "index.php?author_name={$author->nicename}"; } return $author_rewrite; }
and filter author link:
add_filter('author_link', 'my_author_url_with_id', 1000, 2); function my_author_url_with_id($link, $author_id) { $link_base = trailingslashit(get_option('home')); $link = "authors/$author_id"; return $link_base . $link; }
actually don't think need create rules each user in case, following 2 rules should suffice:
add_filter('author_rewrite_rules', 'my_author_url_with_id_rewrite_rules'); function my_author_url_with_id_rewrite_rules($author_rewrite) { $author_rewrite = array(); $author_rewrite["authors/([0-9]+)/page/?([0-9]+)/?$"] = 'index.php?author=$matches[1]&paged=$matches[2]'; $author_rewrite["authors/([0-9]+)/?$"] = 'index.php?author=$matches[1]'; return $author_rewrite; }
Comments
Post a Comment