Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to develop a plugin that allows me to have custom user pages. I really like the way the author archive template handles the URL: /author/username. I don't want to rewrite the author_base as I'd like to keep that functionality in place untouched.

I'd like to replicate that clean URL layout in my plugin, so that when a user goes to /users/username it will go to my custom template. I'd like to know if anyone has examples of how to properly parse the username from the URL so that I can lookup the user's data from the plugin and display my template.

share|improve this question
    
It is not clear to me what you are trying to do. –  s_ha_dum Mar 15 '13 at 14:34
    
I'm trying to make an automated public user page. e.g. tld.com/users/username. Then once that page is loaded, it will load my custom template. So I am trying to figure out how to make the /users/username URL work and pass the username portion of the URL to my plugin. Essentially just like how the author archive page works. –  Pat Mar 15 '13 at 14:38
    
You are going to have /author/username and /users/username? Both on the front-end? And you want the second page controlled entirely by a plugin? No theme edits? –  s_ha_dum Mar 15 '13 at 14:43
    
That sounds like exactly what I'm trying to accomplish. –  Pat Mar 15 '13 at 14:47

1 Answer 1

up vote 3 down vote accepted

I found the answer to this from @bybloggers answer found here. http://wordpress.stackexchange.com/a/58793/12920

I modified his code very slightly to tailor it to my needs, but this is the code that worked for me and was exactly what I was looking for:

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

// Create the rewrites
function userpage_rewrite_rule() {
    add_rewrite_tag( '%member%', '([^&]+)' );
    add_rewrite_rule(
        '^member/([^/]*)/?',
        'index.php?member=$matches[1]',
        'top'
    );
}
add_action('init','userpage_rewrite_rule');

// Catch the URL and redirect it to a template file
function userpage_rewrite_catch() {
    global $wp_query;

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

After this was in my functions.php file, I had to re-save my Permalinks.

Sometimes re-saving the permalinks didn't finish the job 100% and browsing to www.mysite.com/member/username would 404, so I had to manually flush the rules by putting this into my functions.php and loading my site once. Then removing it so I don't run it every time the site loads, since that's unnecessary overhead.

// Code needed to finish the member page setup
function memberpage_rewrite() {
     global $wp_rewrite;
     $wp_rewrite->flush_rules();
}
add_action('init','memberpage_rewrite');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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