How do I create a dynamic page?

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

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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.

    function analytics_rewrite_add_var( $vars ) {
        $vars[] = 'analytic';
        return $vars;
    }
    add_filter( 'query_vars', 'analytics_rewrite_add_var' );
    
    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');
    
    function analytics_rewrite_catch() {
        global $wp_query;
    
        if ( array_key_exists( 'analytic', $wp_query->query_vars ) ) {
            include ( get_stylesheet_directory() . '/html/analytics.php');
            exit;
        }
    }
    add_action( 'template_redirect', 'analytics_rewrite_catch' );