0

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

1 Answer 1

0

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.

3
  • Thank you for your very good and complete explanation. At the moment the address change seems to be working properly and I have no problem with the author's archive. In fact, I also tried whit Permalink, and have brpblem, WordPress replaces the @ character or any other character with the site's domain address. Based on your explanation, can I find another way to do this? For example, by automatically adding this character with code to the beginning of all usernames or nicknames in WordPress?
    – vivi
    Commented Dec 5, 2021 at 8:20
  • Adding it to every username would not be good and cause more problems. my recommendation would be to not do it, instagram/twitter don't do it, and none of the other major social networks do it either. If you try to add it on twitter it'll redirect you to the version without it
    – Tom J Nowell
    Commented Dec 5, 2021 at 14:32
  • Thank you for your recommendation @tom . Yes, you're right. I checked that the big social networks do not do this and I stopped doing it. Thank you very much for your help.
    – vivi
    Commented Dec 6, 2021 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.