0

I am trying to use a custom value to be used in the author posts URL instead of the user_nicename which defaults to user_login in the pretty permalink structure. So far I managed to do that but now the new URL /author/custom_user_nicename generate a 404 error while the old one /author/user_login still works. The code I used in theme functions.php file:

add_filter('author_link', 'update_author_link',10,2);
function update_author_link($link,$author_id) {
    if (FALSE === strpos($link, '?author=')) { // only if pretty permalinks used
        $position = strrpos($link, '/');
        if (strlen($link) == $position + 1) { // check and remove trailing slash
            $link = untrailingslashit($link);
            $position = strrpos($link, '/');
        }
        $link = substr($link, 0, $position+1);
        $link = $link . get_the_author_meta('custom_author_nicename', $author_id);
        return user_trailingslashit($link);
    }
    return $link;
}

I tried to flush the rewrite rules by going to settings -> permalinks and save but that did work as well. do I need to update or modify anything else for the new author URL to work? Thanks.

1 Answer 1

0

You'll need to add a rewrite rule as well as the filter you have written. At the moment your filter only changes what is output when the author_link function is called, not WordPress' permalink structure.

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.