How to create a front end user profile with a friendly permalink

There are two ways I’ve discovered doing this:

  1. Author Page with a custom rewrite rule
  2. A custom template files paired with a rewrite rule

The first is more simple to implement, but may not work in all circumstances (one of which I’ll describe soon).

Custom Rewrite Rule

I found this solution a few days ago here: URL Rewriting

And here’s the code, with comments:

// Define the author levels you want to use
$custom_author_levels = array( 'user', 'leader' );

// On init, add a new author_level rewrite tag and add it to the author_base property of wp_rewrite
add_action( 'init', 'wpleet_init' );
function wpleet_init()
{
    global $wp_rewrite;
    $author_levels = $GLOBALS['custom_author_levels'];

    // Define the tag and use it in the rewrite rule
    add_rewrite_tag( '%author_level%', '(' . implode( '|', $author_levels ) . ')' );
    $wp_rewrite->author_base="%author_level%";

}

// The previous function creates extra author_name rewrite rules that are unnecessary.  
//This function tests for and removes them

add_filter( 'author_rewrite_rules', 'wpleet_author_rewrite_rules' );
function wpleet_author_rewrite_rules( $author_rewrite_rules )
{
    foreach ( $author_rewrite_rules as $pattern => $substitution ) {
        if ( FALSE === strpos( $substitution, 'author_name' ) ) {
            unset( $author_rewrite_rules[$pattern] );
        }
    }
    return $author_rewrite_rules;
}

You can then use the built in author.php template, modifying to your hearts content.

Sincerely, check the link listed above, as Jan Fabry does an excellent job explaining everything.

Query Vars and Page Templates

For the theme I was working on while discovering these solutions, I needed to serve a custom page based on a user meta value (a separate ID). My client did not want the username or user ID visible publicly, so we created a separate layer.

The only problem? At the moment, there isn’t a clear way how to use the Rewrite API to query by meta keys/values. Luckily, there was a solution.

In your functions.php file…

// Create the query var so that WP catches your custom /user/username url
add_filter( 'query_vars', 'wpleet_rewrite_add_var' );
function wpleet_rewrite_add_var( $vars )
{
    $vars[] = 'user';
    return $vars;
}

And then, you need to create a new rewrite tag and rule so that it knows when and how to handle the new query var.

add_rewrite_tag( '%user%', '([^&]+)' );
add_rewrite_rule(
    '^user/([^/]*)/?',
    'index.php?user=$matches[1]',
    'top'
);

Once you’ve done this, you merely need to “catch” when the query var is being served, and then redirect to the template of your choice:

add_action( 'template_redirect', 'wpleet_rewrite_catch' );
function wpleet_rewrite_catch()
{
    global $wp_query;

    if ( array_key_exists( 'user', $wp_query->query_vars ) ) {
        include (TEMPLATEPATH . '/user-profile.php');
        exit;
    }
}

Just make sure you have created user-profile.php.

In my own example, I created a third function that matched the “public User ID” to the actual user_id via the $wpdb->usermeta table, and passed the information to the template.

If you need to create a template different from the rest of your theme, remember that with get_header, you can specify a name:

get_header( 'user' );

Which will call the header-user.php file.

Conclusion

Both of these are valid, working solutions. The second offers a separate layer of “security” as it does not reveal user IDs or usernames, if other people will be able to browse the profiles.

Hope that helps, let me know if you have any questions.

Leave a Comment