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 want to create something similar to what buddypress does with member pages. For eg; http://www.example.com/members/foo

http://www.example.com/members/bar

etc.

I tried looking up the buddypress code and I see that they don't use custom post type or a custom taxonomy. It also doesn't look like they are using add_rewrite_rule() too.

I would like to have something similar, where my plugin will take 'foo' from the URL and generate content on basis of that. What's the best way to do it?

UPDATE:

I followed instructions on this thread, which is exactly what I wanted: How to create a front end user profile with a friendly permalink

Here's the code:

    add_filter( 'query_vars', 'analytics_rewrite_add_var' );
function analytics_rewrite_add_var( $vars )
{
    $vars[] = 'analytic';
    return $vars;
}
function add_analytic_rewrite_rule(){
    add_rewrite_tag( '%analytic%', '([^&]+)' );
    add_rewrite_rule(
        '^analytics/([^/]*)/?',
        'index.php?analytic=$matches[1]',
        'top'
    );
}
add_action('init', 'add_analytic_rewrite_rule');
add_action( 'template_redirect', 'analytics_rewrite_catch' );
function analytics_rewrite_catch()
{
    global $wp_query;

    if ( array_key_exists( 'analytic', $wp_query->query_vars ) ) {
        include ( get_stylesheet_directory() . '/html/analytics.php');
        exit;
    }
}

However, /analytics/foo/ still gives me a 404. What am I doing wrong?

share|improve this question
    
can you clarify ... what is the content on the page you want? if you want a page for each Author (like members in BuddyPress) then this is possible –  Damien Jul 20 '12 at 8:27
    
The content can be anything. For the sake of discussion let say, I just want to echo the last part of the URL, i.e. "foo" or "bar". –  Deepak Mittal Jul 20 '12 at 8:38
    
could it be a get variable rewriten? like ?members=foo to members/foo ? So we can have one page that changes according to the get variable. –  Sisir Jul 20 '12 at 10:10
    
@DeepakMittal please clarify the taxonomy you want example /category/foo or /tag/foo WordPress has to know what foo is. If foo is the TITLE of a post then /foo is just a post or page title –  Damien Jul 20 '12 at 15:51
    
@Damien Its just a random string. It can be anything. And there's no taxonomy. –  Deepak Mittal Jul 20 '12 at 15:55
show 1 more comment

2 Answers

up vote 0 down vote accepted

Here is the answer. And for future references, Deepak, you need to actually post the solution as an answer. Instead, you posted your answer within your own question and then made a comment about it. Please don't do that.

add_filter( 'query_vars', 'analytics_rewrite_add_var' );
function analytics_rewrite_add_var( $vars )
{
$vars[] = 'analytic';
return $vars;
}
function add_analytic_rewrite_rule(){
add_rewrite_tag( '%analytic%', '([^&]+)' );
add_rewrite_rule(
    '^analytics/([^/]*)/?',
    'index.php?analytic=$matches[1]',
    'top'
  );
   }
add_action('init', 'add_analytic_rewrite_rule');
add_action( 'template_redirect', 'analytics_rewrite_catch' );
function analytics_rewrite_catch()
{
global $wp_query;

if ( array_key_exists( 'analytic', $wp_query->query_vars ) ) {
    include ( get_stylesheet_directory() . '/html/analytics.php');
    exit;
}
}
share|improve this answer
    
@Deepak - What you're supposed to do, if you find the solution to your own question, is simply leave the question as the question and post the solution as an answer to your own question. –  Aliyah Jul 21 '12 at 19:31
add comment

Thanks guy. I may have not been clear with my question. I was having this exact same problem: How to create a front end user profile with a friendly permalink

I fixed it with the code I've pasted in my question above. I had forgotten to flush the rules, which MadLem on IRC asked me to do and it's working now.

Sorry for not being clear, and thanks for taking time to answer :)

share|improve this answer
1  
Deepak, I think the code you posted in the update should be inside this Answer instead... –  brasofilo Jul 20 '12 at 16:30
    
Thought copy-pasting same code wouldn't be a good idea. –  Deepak Mittal Jul 21 '12 at 8:21
add comment

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.