How to add @ symbol to user url

I want to add the @ symbol to the beginning of the username in the url i find this code to remove author from url But I do not know how I should change to add @ to the beginning of the usernames and the address will be @authorname or @nickname

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

Topic username Wordpress

Category Web


There's no such thing as a literal @ in that part of a URL, you have to URL encode it. Sometimes browsers will decode it for you so it looks ok but it's just being helpful. The only time you can use it without encoding is when specifying the username and password in a URL e.g. http://admin:[email protected] otherwise it has to be URL encoded as a %40.

If you modify the regular expressions to also match against @ it still won't work because the @ will be converted into %40 before it's sent to the browser.

Thankfully if you try to add the @ in manually WordPress is smart enough to redirect you to the page that is closest, which is usually the author archive of the same user.

Also be wary of that code, removing the /author/ portion of the rule will mean that /test-page/ will also match author archives and you will get clashes that either turn every page into an author archive, or prevent author archives working.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.