Create dynamic page content from custom WordPress plugin page

  1. Create a page for this purpose, which you can filter its content later UNLESS you are working other way.

  2. Add the page ID to the rewrite rule init function:

    add_action('init', function() {
    
        $page = get_post( 2 ); // replace 2 with page ID
    
        add_rewrite_rule(
            '([^/]+)/([^/]+)/?$',
            'index.php?pagename=" . $page->post_name . "&my_param_1=$matches[1]&my_param_2=$matches[2]',
            'top'
        );
    
    });
    
  3. Append the query variables to the main query:

    add_filter('query_vars', function($vars) {
        $vars[] = "my_param_1";
        $vars[] = "my_param_2";
        return $vars;
    });
    

You should be done now and you need to flush the rewrite rules with either flush_rewrite_rules() hooked to init or wp, or go to Settings > Permalinks and hit the save button in the bottom.

Now a test > http://example.com/john/smith/ :

// first param
echo var_dump( get_query_var( 'my_param_1' ) ); // john

// second param
echo var_dump( get_query_var( 'my_param_2' ) ); // smith

I hope that works for you as it did in my local installation.

Edit: It would be much better if you make the parameters go after the page name like $page->post_name . '/([^/]+)/([^/]+)/?$' so you get /sample-page/john/smith/ This way you won’t make any conflicts to existing contents and WordPress will serve the existing content initially before passing to your plugin OR add a unique prefix before the first param slug. Just a thought so.