WordPressサイトでllms.txtを実装する方法を詳しく解説します。プラグインを使った自動生成から、手動での設置方法、カスタマイズまで、WordPress特有の注意点も含めて完全ガイドします。
WordPressでllms.txtを設置する3つの方法
| 方法 | 難易度 | 自動更新 | 推奨度 |
|---|---|---|---|
| 1. プラグイン使用 | 初級 | ○ | ★★★★★ |
| 2. FTPで手動設置 | 初級 | × | ★★★☆☆ |
| 3. functions.phpで生成 | 中級 | ○ | ★★★★☆ |
方法1:プラグインを使用(推奨)
推奨プラグイン
2025年時点では、llms.txt専用のプラグインは少ないですが、以下の方法で実装できます。
オプション1:Custom Upload Dir プラグイン
- 「プラグイン」→「新規追加」
- 「Custom Upload Dir」を検索してインストール
- 有効化
- FTPでルートディレクトリにllms.txtを配置
オプション2:WP Add Custom File プラグイン
- 「プラグイン」→「新規追加」
- 「WP Add Custom File」を検索
- インストール・有効化
- プラグイン設定でllms.txtを追加
自作プラグインを作成(推奨)
シンプルな自作プラグインを作成することで、自動生成が可能です。
プラグインファイルの作成
" . $site_description . "\n\n";
// 固定ページを取得
$pages = get_pages();
if ($pages) {
$content .= "## ページ\n\n";
foreach ($pages as $page) {
$content .= "- [" . $page->post_title . "](" . get_permalink($page->ID) . "): " . wp_trim_words($page->post_excerpt, 20) . "\n";
}
$content .= "\n";
}
// 最新記事を取得
$recent_posts = wp_get_recent_posts(array('numberposts' => 5));
if ($recent_posts) {
$content .= "## 最新記事\n\n";
foreach ($recent_posts as $post) {
$content .= "- [" . $post['post_title'] . "](" . get_permalink($post['ID']) . ")\n";
}
$content .= "\n";
}
return $content;
}
// llms.txtを生成・保存
function save_llms_txt() {
$content = generate_llms_txt();
$file_path = ABSPATH . 'llms.txt';
file_put_contents($file_path, $content);
}
// 記事公開時に自動更新
add_action('publish_post', 'save_llms_txt');
add_action('publish_page', 'save_llms_txt');
// プラグイン有効化時に生成
register_activation_hook(__FILE__, 'save_llms_txt');
?>プラグインの設置手順
- 上記コードを
llms-txt-generator.phpとして保存 - FTPで
/wp-content/plugins/llms-txt-generator/フォルダを作成 - その中に
llms-txt-generator.phpをアップロード - WordPress管理画面の「プラグイン」から有効化
方法2:FTPで手動設置
手順
- ローカルでllms.txtを作成
- FTPソフト(FileZilla等)でWordPressサイトに接続
- WordPressのルートディレクトリを開く(wp-config.phpがある場所)
- llms.txtをアップロード
WordPressのルートディレクトリ例
/
├── wp-admin/
├── wp-content/
├── wp-includes/
├── index.php
├── wp-config.php
├── llms.txt ← ここに配置
└── ...注意点
- wp-content/フォルダ内ではない
- テーマフォルダ内でもない
- WordPressルート(最上位ディレクトリ)に配置
方法3:functions.phpで動的生成
テーマのfunctions.phpに追加することで、動的にllms.txtを生成できます。
コード例
// テーマのfunctions.phpに追加
// llms.txtのエンドポイントを追加
add_action('init', 'add_llms_txt_endpoint');
function add_llms_txt_endpoint() {
add_rewrite_rule('^llms\.txt$', 'index.php?llms_txt=1', 'top');
}
// クエリ変数を追加
add_filter('query_vars', 'add_llms_txt_query_var');
function add_llms_txt_query_var($vars) {
$vars[] = 'llms_txt';
return $vars;
}
// llms.txtを動的生成
add_action('template_redirect', 'generate_llms_txt_content');
function generate_llms_txt_content() {
if (get_query_var('llms_txt')) {
header('Content-Type: text/plain; charset=utf-8');
$site_name = get_bloginfo('name');
$site_description = get_bloginfo('description');
echo "# " . $site_name . "\n\n";
echo "> " . $site_description . "\n\n";
// 固定ページ
$pages = get_pages(array('sort_column' => 'menu_order'));
if ($pages) {
echo "## ページ\n\n";
foreach ($pages as $page) {
$excerpt = $page->post_excerpt ? $page->post_excerpt : wp_trim_words(strip_tags($page->post_content), 20);
echo "- [" . $page->post_title . "](" . get_permalink($page) . "): " . $excerpt . "\n";
}
echo "\n";
}
// カテゴリ
$categories = get_categories(array('hide_empty' => true));
if ($categories) {
echo "## カテゴリ\n\n";
foreach ($categories as $category) {
echo "- [" . $category->name . "](" . get_category_link($category) . "): " . $category->description . "\n";
}
echo "\n";
}
// 最新記事
$recent_posts = wp_get_recent_posts(array('numberposts' => 10));
if ($recent_posts) {
echo "## 最新記事\n\n";
foreach ($recent_posts as $post) {
echo "- [" . $post['post_title'] . "](" . get_permalink($post['ID']) . ")\n";
}
}
exit;
}
}
// パーマリンク設定を保存した時に、リライトルールをフラッシュ
add_action('after_switch_theme', 'flush_llms_txt_rewrite');
function flush_llms_txt_rewrite() {
add_llms_txt_endpoint();
flush_rewrite_rules();
}設定後の確認
- 「設定」→「パーマリンク」を開く
- 何も変更せず「変更を保存」をクリック(リライトルールの更新)
- https://yoursite.com/llms.txt にアクセスして確認
WordPressでのカスタマイズ
特定のページを除外
// 特定のページIDを除外
$pages = get_pages(array(
'exclude' => array(123, 456), // 除外するページID
'sort_column' => 'menu_order'
));カスタム投稿タイプを追加
// カスタム投稿タイプ「portfolio」を追加
$portfolios = get_posts(array(
'post_type' => 'portfolio',
'numberposts' => 10
));
if ($portfolios) {
echo "## ポートフォリオ\n\n";
foreach ($portfolios as $portfolio) {
echo "- [" . $portfolio->post_title . "](" . get_permalink($portfolio) . ")\n";
}
echo "\n";
}ACFカスタムフィールドを活用
// ACFで設定した説明文を使用
$pages = get_pages();
foreach ($pages as $page) {
$custom_desc = get_field('llms_description', $page->ID);
$description = $custom_desc ? $custom_desc : wp_trim_words($page->post_excerpt, 20);
echo "- [" . $page->post_title . "](" . get_permalink($page) . "): " . $description . "\n";
}マルチサイトでの対応
WordPress マルチサイトの場合、各サイトごとにllms.txtを生成できます。
// 各サイトのllms.txtを生成
add_action('template_redirect', 'generate_multisite_llms_txt');
function generate_multisite_llms_txt() {
if (get_query_var('llms_txt')) {
header('Content-Type: text/plain; charset=utf-8');
$blog_id = get_current_blog_id();
$site_name = get_bloginfo('name');
echo "# " . $site_name . " (Site ID: " . $blog_id . ")\n\n";
echo "> " . get_bloginfo('description') . "\n\n";
// 以下、通常の生成処理
exit;
}
}パフォーマンス最適化
キャッシュの活用
// Transient APIでキャッシュ
function get_cached_llms_txt() {
$cache_key = 'llms_txt_content';
$cached = get_transient($cache_key);
if ($cached !== false) {
return $cached;
}
// llms.txtを生成
$content = generate_llms_txt();
// 24時間キャッシュ
set_transient($cache_key, $content, DAY_IN_SECONDS);
return $content;
}
// 記事更新時にキャッシュをクリア
add_action('save_post', 'clear_llms_txt_cache');
function clear_llms_txt_cache() {
delete_transient('llms_txt_content');
}静的ファイルとして保存
// 記事更新時に静的ファイルとして保存
function save_llms_txt_static() {
$content = generate_llms_txt();
$file_path = ABSPATH . 'llms.txt';
file_put_contents($file_path, $content);
}
add_action('save_post', 'save_llms_txt_static');
add_action('delete_post', 'save_llms_txt_static');セキュリティ対策
1. 非公開コンテンツを除外
// 公開済みのコンテンツのみ取得
$pages = get_pages(array(
'post_status' => 'publish'
));
$posts = get_posts(array(
'post_status' => 'publish',
'numberposts' => 10
));2. パスワード保護ページを除外
// パスワード保護されているページを除外
$pages = get_pages();
foreach ($pages as $page) {
if (!post_password_required($page->ID)) {
echo "- [" . $page->post_title . "](" . get_permalink($page) . ")\n";
}
}3. 管理者専用ページを除外
// 特定のカテゴリやタグを除外
$posts = get_posts(array(
'category__not_in' => array(999), // 除外するカテゴリID
'numberposts' => 10
));プラグインとの互換性
Yoast SEOとの連携
// Yoast SEOのメタディスクリプションを使用
if (function_exists('wpseo_auto_load')) {
$meta_desc = get_post_meta($page->ID, '_yoast_wpseo_metadesc', true);
$description = $meta_desc ? $meta_desc : wp_trim_words($page->post_excerpt, 20);
}WooCommerceとの連携
// 商品ページを追加
if (class_exists('WooCommerce')) {
$products = wc_get_products(array(
'limit' => 10,
'status' => 'publish'
));
echo "## 商品\n\n";
foreach ($products as $product) {
echo "- [" . $product->get_name() . "](" . $product->get_permalink() . "): " . $product->get_short_description() . "\n";
}
echo "\n";
}トラブルシューティング
問題1:llms.txtが404エラー
解決方法
- 「設定」→「パーマリンク」→「変更を保存」
- キャッシュプラグインをクリア
- .htaccessを確認
問題2:内容が更新されない
解決方法
- キャッシュプラグインを無効化
- ブラウザキャッシュをクリア
- Transientキャッシュを削除
delete_transient('llms_txt_content');
問題3:パーミッションエラー
解決方法
// ファイル書き込み権限を確認
if (is_writable(ABSPATH)) {
file_put_contents(ABSPATH . 'llms.txt', $content);
} else {
error_log('llms.txt: Directory not writable');
}まとめ:WordPress に最適な方法
初心者向け
- FTPでllms.txtを手動設置
- 更新時に手動で編集
中級者向け
- 自作プラグインで自動生成
- 記事更新時に自動更新
上級者向け
- functions.phpで動的生成
- キャッシュを活用
- カスタム投稿タイプも含める
余日(Yojitsu)のWordPress llms.txt導入サポート
余日では、WordPressサイトへのllms.txt実装をサポートしています。自動生成プラグインの設置から、カスタマイズまで対応可能です。
参考文献・データソース
本記事で紹介したWordPressでのllms.txt実装方法は、以下の信頼できる情報源に基づいています(2024-2025年):
- llmstxt.org
llms.txtの公式仕様・実装ガイドライン
https://llmstxt.org/ - WordPress Codex・Developer Resources
WordPressの公式ドキュメント・APIリファレンス
https://developer.wordpress.org/ - WordPress Plugin Handbook
プラグイン開発・functions.phpの公式ガイド
https://developer.wordpress.org/plugins/ - WordPress Rewrite API
URLリライト・カスタムエンドポイントに関する公式ドキュメント
https://developer.wordpress.org/reference/classes/wp_rewrite/