1

I am using custom urls for my author pages like this:

http://site.com/author_name

After I create a new user, that author page appears as not found; however, if I update my permalink structure from admin panel, the author page is found.

So, is it necessary to flush rewrite rules after a new user is created? Is there any other way?

From codex, I read that flushing rewrite rules is a very expensive task. So, I would like to avoid this. Is it possible?

EDIT:

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

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;
}
  • where is the code you are using to enable these URLs? – Milo Apr 18 '14 at 4:01
  • @Milo I edited my question to include the code. – – Gixty Apr 18 '14 at 4:39
2

Yes, you should flush rewrite rules in this case, because you add rules for every author based on his nicename.

To be more precise, you should flush them after any author changes his nicename also.

And you do this using author_rewrite_rules hook, which is fired in wp_rewrite_rules() only when there are no rules in database. So without flushing them your filter won't be fired and your new rules won't be added.

| improve this answer | |
  • Users are not able to change their usernames so no need there. However, what would happen if I have a million users registered? The next time a user registers, wouldn't it take a long time to flush and rewrite all author rules thus making the site very slow? By the way, I tried to change the author url base, like this $wp_rewrite->author_base = ""; it works for authors, but other links are broken. – Gixty Apr 18 '14 at 16:30
  • Yes, it would take a long time to flush them. But there is another problem - it would take a lot of space in your database. You add 2 rules for every user and rule is approx. 75 characters long, so for 1 million users it would take around 150 MB of data just to store these rules... – Krzysiek Dróżdż Apr 18 '14 at 17:26
  • I see. Well, I just removed that code and implemented this $wp_rewrite->author_base = ""; Then, I just had to add rewrite rules for each page, these wont go over 20. Thank you for the support. – Gixty Apr 18 '14 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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