How to display a raw HTML page (bypassing WordPress theme, scripts, etc)

If you’re willing to create a child theme and put your PHP files in there, this approach is relatively clean. For each file, create a rewrite rule where you specify the path you want to use and the filename that should load. In the example below, /custom-path/ loads custom-file.php from the root of your child theme directory and /custom-path-2/ loads custom-file-2.php. Don’t forget to flush permalinks after adding rewrites.

/*
 * Set up a rewrite for each path which should load a file.
 */
function my_standalone_rewrites() {
    add_rewrite_rule( '^custom-path/?', 'index.php?standalone=custom-file', 'top' );
    add_rewrite_rule( '^custom-path-2/?', 'index.php?standalone=custom-file-2', 'top' );
}
add_action( 'init', 'my_standalone_rewrites' );

/*
 * Make `standalone` available as a query var.
 */
function my_query_vars( $vars ) {
  $vars[] = 'standalone';
  return $vars;
}
add_filter( 'query_vars', 'my_query_vars' );

/*
 * If `standalone` is set when parsing the main query, load the standalone file.
 */
function my_standalone_path( &$wp_query ) {
    if ( $wp_query->is_main_query() && get_query_var( 'standalone', false ) ) {
        // Load filename.php from your child theme root.
        get_template_part( get_query_var( 'standalone' ) );
        exit;
    }
}
add_action( 'parse_query', 'my_standalone_path' );

Leave a Comment