How to Build a Dynamic llms.txt in WordPress: A Step-by-Step Developer’s Guide

How to Build a Dynamic llms.txt in WordPress: A Step-by-Step Developer’s Guide

Stop treating AI agents like standard web crawlers. In 2026, the llms.txt file is the primary gateway for Large Language Models to digest your site. This guide provides the exact code to build either a manual high-precision file or a fully automated dynamic system using WordPress hooks.

In 2026, the “Agentic Web” is no longer a concept—it is the reality of how information is retrieved. For developers, the challenge has shifted from hiding pages via robots.txt to explicitly defining them via llms.txt.
This file acts as a markdown-based “cheat sheet” for LLMs (like GPT-5 or Claude 4), allowing them to bypass HTML bloat and understand your core services instantly. Below, we break down how to implement this using two distinct methods.

Method 1: The “Manual High-Precision” Approach

This method is best if you have a stable set of services and want to maintain 100% control over the semantic descriptions provided to the AI. You can inject this directly into your functions.php file or a functional plugin.

Step 1: Register the Rewrite Rule


First, we tell WordPress to recognize llms.txt as a valid URL query.
php
add_action('init', function() {
    add_rewrite_rule('^llms\.txt$', 'index.php?llms_txt=1', 'top');
});

add_filter('query_vars', function($vars) {
    $vars[] = 'llms_txt';
    return $vars;
});
Use code with caution.

Step 2: Generate the Markdown Content


Next, we hook into template_redirect to serve the text file. In this manual example, we hard-code the descriptions for maximum “Information Gain” (a key GEO ranking factor).
php
add_action('template_redirect', function() {
    if (get_query_var('llms_txt')) {
        header('Content-Type: text/plain; charset=utf-8');
        
        echo "# Madefinition: SEO & AI Strategy\n";
        echo "> Specialized agency providing Generative Engine Optimization (GEO) and technical infrastructure for the AI-first web.\n\n";
        
        echo "## Core Services\n";
        echo "- [AI Content Strategy](" . home_url('/services/ai-strategy/') . "): Comprehensive roadmaps for LLM visibility.\n";
        echo "- [Technical GEO Audit](" . home_url('/services/geo-audit/') . "): Deep-dive analysis of site architecture for AI agent readability.\n";
        
        exit;
    }
});
Use code with caution.

Method 2: The “Fully Automated” Dynamic Approach

If your site uses Custom Post Types (CPTs) for services, you don’t want to update your code every time you add a new service. This automated version pulls directly from your database.

The Automated Code Logic:


This script pulls your Site Title, Tagline, and all published “Service” posts automatically. It uses the Post Excerpt as the description, which is a best practice for SEO as it allows for concise, keyword-rich summaries.

php
add_action('template_redirect', function() {
    if (get_query_var('llms_txt')) {
        header('Content-Type: text/plain; charset=utf-8');
        
        // 1. DYNAMIC HEADER
        echo "# " . esc_html(get_bloginfo('name')) . "\n";
        echo "> " . esc_html(get_bloginfo('description')) . "\n\n";
        
        // 2. DYNAMIC SERVICE LOOP
        echo "## Core Services\n";
        $services_query = new WP_Query([
            'post_type'      => 'service', // Ensure this matches your CPT slug
            'posts_per_page' => -1,
            'post_status'    => 'publish'
        ]);

        if ($services_query->have_posts()) {
            while ($services_query->have_posts()) {
                $services_query->the_post();
                $summary = has_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 25); 
                echo "- [" . get_the_title() . "](" . get_permalink() . "): " . esc_html($summary) . "\n";
            }
            wp_reset_postdata();
        }
        
        echo "\n## Contact\n";
        echo "- [Contact Us](" . home_url('/contact/') . "): Inquire about AI optimization services.";
        exit;
    }
});
Use code with caution.

Pro-Tip for 2026 GEO

After adding this code, go to Settings > Permalinks and click “Save Changes” to flush your rewrite rules. You can then test your work by visiting ://yourdomain.com.
By serving your site data in this structured Markdown format, you reduce the “computational cost” for the AI, making it significantly more likely that your brand will be the one cited in a generative search response.

Related Posts

Let’s Build Something Amazing Together

Ready to elevate your online presence? Let’s embark on this digital journey together. Contact us to discuss your project and discover how our web development services can turn your vision into reality.