次のような「特定の書式(主にファイル先頭のコメントヘッダーやファイル名規則)」でPHPファイルを書くと、WordPress本体のUIや表示ロジックに直接影響します。
目次
1) 固定ページテンプレート(Page Template)
- 影響:編集画面「テンプレート」プルダウンに表示/選択可能になり、選ばれたテンプレートでフロントの表示が変わります。
- 書式(PHPファイル先頭のコメントヘッダー):
<?php
/*
Template Name: 1カラム固定(サイドバー無し)
Template Post Type: page, post // 任意:対応ポストタイプを限定
*/公式ドキュメント:
- Developer Resources – Page Templates
https://developer.wordpress.org/themes/classic-themes/templates/page-template-files/ - WordPress Developer Resources – PHP Coding Standards(Closing PHP tag)
https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#closing-php-tag - php.net PHP Manual – Instruction separation / PHP tags(末尾の閉じタグは任意)
https://www.php.net/manual/en/language.basic-syntax.instruction-separation.php
関連:当サイトのテーマSWELLのカスタムテンプレートの作り方についてはこちらで記事にしています。
Ai0w.com
ワードプレス(テーマ SWELL:ブロックテーマ)で固定ページの「カスタムテンプレート」を作るには? | Ai0w…
ブロックテーマの場合 子テーマ にテンプレートを追加します。 WordPress Developer Resources テンプレート 配置場所はテーマにより異なります。基本は公式「Templates」…
2) プラグイン・メインファイルのヘッダー
- 影響:管理画面「プラグイン一覧」での名称・説明・バージョン等の表示、認識。
- 最低限必要:
Plugin Name。その他、Description,Version,Requires at least,Requires PHP,Text Domainなど。 - 書式:
<?php
/*
* Plugin Name: My Sample Plugin
* Plugin URI: https://example.com/plugins/the-sample/
* Description: Handle the basics with this plugin.
* Version: 1.10.3
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Sample Name
* Author URI: https://author.example.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://example.com/my-plugin/
* Text Domain: my-sample -plugin
* Domain Path: /languages
* Requires Plugins: my-plugin, yet-another-plugin
*/- 公式ドキュメント:
Developer Resources – Header Requirements (Plugin)
https://developer.wordpress.org/plugins/plugin-basics/header-requirements/
3) テーマ内「パターン(/patterns/*.php)」のヘッダー
- 影響:エディタ内の「パターン」選択モーダルや、スターターパターンとしての表示に反映。
- 代表的フィールド:
Title,Slug,Categories,Block Types,Post Types,Template Typesなど。 - 書式例(/patterns/example.php):
<?php
/**
* Title: About
* Slug: sample/page-about-business
* Categories: page
* Keywords: starter
* Block Types: core/post-content
* Post Types: page, wp_template
* Viewport width: 1400
*/
?>
<!-- Block code here. -->- 公式ドキュメント:
Developer Blog – Adding starter patterns to your WordPress themes
https://developer.wordpress.org/news/2024/01/adding-starter-patterns-to-your-wordpress-themes/ - 補足(パターンの登録やPHP利用の制限点):
- Theme Handbook – Registering Patterns
https://developer.wordpress.org/themes/patterns/registering-patterns/ - Theme Handbook – Using PHP in Patterns
https://developer.wordpress.org/themes/patterns/using-php-in-patterns/
4) テンプレート階層(ファイル名のフォーマット)
- 影響:フロント表示時にどのテンプレートが読み込まれるかをコアが自動決定
(例:category-news.php→category.php→archive.php→index.phpの順)。 - 例:
- カテゴリースラッグ
newsならcategory-news.phpが最優先 - 投稿IDが4のカテゴリなら
category-4.phpも対象
- カテゴリースラッグ
- 公式ドキュメント:
Developer Resources – Template Hierarchy
https://developer.wordpress.org/themes/classic-themes/basics/template-hierarchy/
よくある注意点
- ヘッダーはファイル先頭(最初の数KB以内)に置くこと。パースされない位置だと認識されません(特にテンプレート/プラグイン)。 WordPress Developer Resources:Header Requirements
Template Post Typeを付けると、特定の投稿タイプにだけテンプレートを出すことができます。 WordPress Developer Resources:Page Templates- パターンのヘッダーは
/patternsディレクトリ直下のPHPファイルに書くと自動登録され、スターター/テンプレートパターンとしてモーダルに出せます。 WordPress Developer Resources:patterns
まとめ
- 「コメントヘッダーのメタデータ」(テンプレート、プラグイン、パターン)と、「ファイル名の規則」(テンプレート階層)が、WP本体側のUI・読み込みロジック・表示に直接影響します。上の書式を守れば、狙いどおり管理画面やフロントの挙動をコントロールできます。
コメント