Create dynamic page in WordPress using WP Rewrite

Last Updated: January 28, 2019

function custom_rewrite_basic() {
    add_rewrite_rule('^page/([^/]*)/', 'index.php?parameter=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');


function prefix_register_query_var( $vars ) {
    $vars[] = 'parameter';
    return $vars;
}

add_filter( 'query_vars', 'prefix_register_query_var' );

function prefix_url_rewrite_templates() {
 
    if ( get_query_var( 'parameter' )) {
    
        $GLOBALS['value'] = 'value';
        
        add_filter( 'template_include', function() {
            return get_template_directory() . '/page-templates/your-custom-template.php';
        });
        
    }
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );

If there are any questions or suggestions, feel free to contact me.

Add your feedback or comment below: