Skip to content

Instantly share code, notes, and snippets.

@kodie
Last active August 21, 2020 16:36
Show Gist options
  • Save kodie/02fa54ac08017a87dca0b7f64fe643c0 to your computer and use it in GitHub Desktop.
Save kodie/02fa54ac08017a87dca0b7f64fe643c0 to your computer and use it in GitHub Desktop.
Enables https://mywebsite.com/@username for author URLs in WordPress
<?php
// Enables https://mywebsite.com/@username for author URLs
add_action('init', 'custom_author_url_rewrite');
function custom_author_url_rewrite() {
add_rewrite_rule('^@([a-zA-Z0-9.-_]+)/?', 'index.php?author_name=$matches[1]', 'top');
}
// Changes get_author_posts_url() response to https://mywebsite.com/@username author URLs
add_filter('author_link', 'custom_author_url_filter', 10, 3);
function custom_author_url_filter($link, $author_id, $author_nicename) {
$user = get_user_by('id', $author_id);
return network_home_url("/@{$user->user_login}");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment