Adding more pages to author pages

At the moment I have the following code for changing the author_base slug.

/** ADD REWRITE RULES **/
function change_author_permalinks() {
   global $wp_rewrite;

   $wp_rewrite->author_base = 'profile';
   $wp_rewrite->flush_rules();
}
add_action('init','change_author_permalinks');

How can I add, for example, a extra “edit” page on to this, i.e., /profile/my-user-name/edit ???

Related posts

Leave a Reply

1 comment

  1. The author rewrite rules are filtered through author_rewrite_rules. You can add a rule there for the pattern author/([^/]+)/edit/?$, but the substitution will depend on how you want to create the edit page. A simple example that will set a custom query variable and load a specific template if this variable is set:

    add_action( 'author_rewrite_rules', 'wpse18547_author_rewrite_rules' );
    function wpse18547_author_rewrite_rules( $author_rules )
    {
        $author_rules['author/([^/]+)/edit/?$'] = 'index.php?author_name=$matches[1]&wpse18547_author_edit=1';
        return $author_rules;
    }
    
    add_filter( 'query_vars', 'wpse18547_query_vars' );
    function wpse18547_query_vars( $query_vars )
    {
        $query_vars[] = 'wpse18547_author_edit';
        return $query_vars;
    }
    
    add_filter( 'author_template', 'wpse18547_author_template' );
    function wpse18547_author_template( $author_template )
    {
        if ( get_query_var( 'wpse18547_author_edit' ) ) {
            return locate_template( array( 'edit-author.php', $author_template ) );
        }
        return $author_template;
    }
    

    Small tip: Don’t call flush_rules() on every init, it’s an expensive operation. You only need to do it when the rewrite rules change. You can manually flush the rules by just visiting the Permalinks settings page. And if you are going to play with the rewrite rules, I recommend you install my Rewrite analyzer plugin.