Creating a virtual frontend URL in WordPress without a physical page (e.g. for llms.txt)

Step 1 — Create a template file

Note: For simple requests, such as llms.txt, creating template file is not required. Skip to Production example: Virtual /llms.txt endpoint without template file

Inside your plugin or theme, create a folder:

/templates/

Then create a file:

/templates/custom.php

Add temporary test content:

<?php
echo "Hi";

Step 2 — Register rewrite rule + query var + template loader

Add this to your plugin or theme:

add_filter( 'generate_rewrite_rules', function ( $wp_rewrite ) {

    $wp_rewrite->rules = array_merge(
        [
            'mypage/?$' => 'index.php?custom=1',
        ],
        $wp_rewrite->rules
    );

} );

add_filter( 'query_vars', function( $query_vars ) {

    $query_vars[] = 'custom';
    return $query_vars;

} );

add_action( 'template_redirect', function(){

    $custom = intval( get_query_var( 'custom' ) );

    if ( $custom ) {
        include plugin_dir_path( __FILE__ ) . 'templates/custom.php';
        die;
    }

} );

⚠️ Note: plugin_dir_path( __FILE__ ) may need adjustment depending on where the template file lives.


Go to: WP Admin → Settings → Permalinks, and click Save (no changes needed).
This regenerates rewrite rules.


Step 4 — Test It

Visit:

https://yoursite.com/mypage/

If it doesn’t work immediately:

  • Clear browser cache
  • Try incognito
  • Confirm permalinks were flushed

Step 5 — Replace test content

Now remove the temporary echo "Hi"; and add the real content to the template file.

Production example: Virtual /llms.txt endpoint without template file

At Rareview, we implemented a virtual frontend endpoint for:

https://rareview.com/llms.txt

This file does not physically exist — it is generated dynamically. Below is the real-world implementation:

<?php
/**
 * Virtual frontend URL for llms.txt
 *
 * @package Rareview
 /

namespace Rareview\Inc\Theme;

class Llms {

	public function __construct() {
		add_filter( 'generate_rewrite_rules', [ $this, 'add_rewrite_rule' ] );
		add_filter( 'query_vars', [ $this, 'register_query_var' ] );
		add_action( 'template_redirect', [ $this, 'render_llms_txt' ] );
		add_filter( 'redirect_canonical', [ $this, 'remove_end_slash' ], 10, 2 );
	}

	public function add_rewrite_rule( \WP_Rewrite $wp_rewrite ): \WP_Rewrite {
		$wp_rewrite->rules = array_merge(
			[
				'llms\.txt$' => 'index.php?llms_txt=1',
			],
			$wp_rewrite->rules
		);

		return $wp_rewrite;
	}

	public function register_query_var( array $query_vars ): array {
		$query_vars[] = 'llms_txt';
		return $query_vars;
	}

	public function render_llms_txt(): void {
		if ( ! get_query_var( 'llms_txt' ) ) {
			return;
		}

		header( 'Content-Type: text/plain; charset=utf-8' );
		status_header( 200 );

		echo $this->get_llms_content();

		exit;
	}

	private function get_llms_content(): string {
		return <<<TXT
			# Rareview Agency Profile
			Add content here...
			TXT;
	}

	public function remove_end_slash( $redirect_url, $requested_url ): string|false {
		if ( str_ends_with( $requested_url, '/llms.txt' ) ) {
			return false;
		}

		return $redirect_url;
	}
}

Table of Contents