Contents
1. ローカル開発環境の準備
| 項目 | 推奨ツール・バージョン | 設定ポイント |
|---|---|---|
| Web サーバ/DB | XAMPP(PHP 8.2 / MySQL 8) または LocalWP(最新版) |
Apache と MySQL が有効か確認 |
| PHP | 8.1 以上(WordPress.org が推奨する最小バージョンは 7.4、最新安定版の利用が推奨)[^php] | php -v でバージョンを確認。XAMPP の場合は php.ini で extension_dir 等を調整 |
| Composer | 2.x 系列(公式インストーラ推奨)[^composer] | composer --version で動作確認 |
| データベースクライアント | phpMyAdmin、または CLI (mysql -u root -p) |
DB 名は wp_custom_plugin としておく |
手順概要
- ローカルサーバをインストールし、Apache が起動していることを確認。
- PHP バージョンが 8.0 以上であることを
php -vでチェック。バージョンが古い場合は XAMPP のphp.iniを差し替えるか、Homebrew 等で最新 PHP をインストール。 - WordPress 本体を取得
bash
curl -O https://wordpress.org/latest.zip
unzip latest.zip -d /path/to/htdocs/your-site - データベース作成(phpMyAdmin で「wp_custom_plugin」を新規作成、ユーザーは
root/空パスワードでも可)。
ポイント:WordPress の公式要件は常に変わります。最新情報はWordPress.org のサーバー要件ページをご参照ください。
wp-config.php の最小設定
|
1 2 3 4 5 6 7 8 9 10 |
<?php define( 'DB_NAME', 'wp_custom_plugin' ); define( 'DB_USER', 'root' ); define( 'DB_PASSWORD', '' ); define( 'DB_HOST', 'localhost' ); define( 'WP_DEBUG', true ); // 開発時は必ず有効化 define( 'WP_DEBUG_LOG', true ); // debug.log に出力 define( 'WP_DEBUG_DISPLAY', false ); // 画面表示は抑制 |
デバッグ情報は wp-content/debug.log に蓄積され、ターミナルで tail -f wp-content/debug.log とすればリアルタイムに確認できます。
2. プラグイン雛形の作成
ディレクトリ構造(推奨)
|
1 2 3 4 5 6 7 8 9 |
wp-content/ └─ plugins/ └─ modern-plugin/ ├─ src/ # PHP クラス ├─ assets/ # JS / CSS ビルド成果物 ├─ composer.json ├─ modern-plugin.php # エントリーポイント └─ README.txt |
必須ヘッダー(modern-plugin.php の冒頭)
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php /** * Plugin Name: Modern WP Custom Plugin * Description: 名前空間・PSR‑4 に対応した雛形。WordPress 6.x / PHP 8.1+ 向け。 * Version: 1.0.0 * Author: Your Name * Requires at least: 6.0 * Requires PHP: 8.1 * Text Domain: modern-plugin */ |
注意:
Requires PHPのバージョンは実際に動作確認したものを記載してください。公式情報はWordPress Plugin Header Docsで公開されています。
3. 名前空間と Composer による PSR‑4 オートローディング
composer.json のサンプル
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ "name": "yourname/modern-plugin", "description": "A modern starter kit for WordPress plugins.", "type": "wordpress-plugin", "require": { "php": "^8.1" }, "autoload": { "psr-4": { "ModernPlugin\\": "src/" } }, "scripts": { "post-install-cmd": [ "@php -r \"file_exists('vendor/autoload.php') || exit(0);\"" ] } } |
- プラグインルートで
composer installを実行 →vendor/autoload.phpが生成。 - エントリーファイルでオートローダを読み込む:
|
1 2 |
require_once __DIR__ . '/vendor/autoload.php'; |
クラス例:src/Activator.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace ModernPlugin; class Activator { public static function activate(): void { if ( ! get_option( 'modern_plugin_version' ) ) { add_option( 'modern_plugin_version', '1.0.0' ); } } public static function deactivate(): void { // 必要に応じてクリーンアップ } } |
名前空間 ModernPlugin とディレクトリ src/ が 1 対 1 に対応していることが PSR‑4 の基本です。
4. 基本機能の実装(フック・ショートコード・CPT)
アクティベーション / デアクティベーション
|
1 2 3 4 5 |
use ModernPlugin\Activator; register_activation_hook( __FILE__, [ Activator::class, 'activate' ] ); register_deactivation_hook( __FILE__, [ Activator::class, 'deactivate' ] ); |
ショートコード例(現在日時表示)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace ModernPlugin; class Shortcode { public static function now(): string { return sprintf( '<span class="mp-now">%s</span>', esc_html( wp_date( 'Y-m-d H:i:s' ) ) ); } } // 登録はエントリーファイル側で add_shortcode( 'mp_now', [ Shortcode::class, 'now' ] ); |
[mp_now]を投稿に埋め込むだけで、ローカライズ済みのサーバ時刻が安全に出力されます。
カスタム投稿タイプ(CPT)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace ModernPlugin; class CPT { public static function register(): void { $args = [ 'label' => __( 'Books', 'modern-plugin' ), 'public' => true, 'has_archive' => true, 'show_in_rest' => true, // Gutenberg と REST API に必須 'supports' => [ 'title', 'editor', 'thumbnail' ], 'rewrite' => [ 'slug' => 'books' ], ]; register_post_type( 'mp_book', $args ); } } add_action( 'init', [ CPT::class, 'register' ] ); |
5. スクリプト・スタイルの enqueue と Gutenberg ブロック連携
アセット登録クラス
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace ModernPlugin; class Assets { public static function register(): void { $ver = filemtime( plugin_dir_path( __DIR__ ) . 'assets/build/main.js' ); wp_register_script( 'modern-plugin-block', plugins_url( 'assets/build/main.js', __DIR__ ), [ 'wp-blocks', 'wp-element', 'wp-i18n' ], $ver, true ); wp_register_style( 'modern-plugin-editor', plugins_url( 'assets/build/editor.css', __DIR__ ), [ 'wp-edit-blocks' ], $ver ); } public static function enqueue(): void { wp_enqueue_script( 'modern-plugin-block' ); wp_enqueue_style( 'modern-plugin-editor' ); } } add_action( 'enqueue_block_editor_assets', [ Assets::class, 'register' ] ); add_action( 'enqueue_block_editor_assets', [ Assets::class, 'enqueue' ] ); |
filemtime() をバージョンに使うと、ファイルが更新された際に自動でキャッシュが破棄されます。
npm / webpack ビルド例
package.json(抜粋)
|
1 2 3 4 5 6 7 8 9 10 11 |
{ "name": "modern-plugin-block", "scripts": { "build": "webpack --mode production", "watch": "webpack --watch" }, "devDependencies": { "@wordpress/scripts": "^26.0.0" } } |
webpack.config.js
|
1 2 3 4 5 6 7 8 9 10 11 |
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' ); module.exports = { ...defaultConfig, entry: './src/index.js', output: { path: __dirname + '/build', filename: 'main.js', }, }; |
ブロック登録(modern-plugin.php)
|
1 2 3 4 5 6 7 |
add_action( 'init', function () { register_block_type( 'modern-plugin/sample-block', [ 'editor_script' => 'modern-plugin-block', // 必要に応じて style / editor_style も指定可能 ] ); } ); |
@wordpress/scriptsは WordPress が公式に提供しているビルドツールで、依存パッケージ(wp-element,wp-blocksなど)を自動解決します。詳しくはWordPress Scripts のドキュメントをご覧ください。
6. デバッグ・テスト・CI/CD
WP_DEBUG の活用
|
1 2 3 4 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( '-- Debug start: ' . current_time( 'mysql' ) . ' --' ); } |
tail -f wp-content/debug.log でリアルタイムにログを確認できます。
PHPUnit によるユニットテスト
- 開発依存パッケージのインストール
bash
composer require --dev phpunit/phpunit wp-phpunit/wp-phpunit
phpunit.xmlの雛形
xml
- テストケース例(
tests/test-activator.php)
php
assertSame( '1.0.0', get_option( 'modern_plugin_version' ) );
}
}
実行は vendor/bin/phpunit。
### GitHub Actions での CI/CD パイプライン
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
name: CI on: push: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # PHP と Composer のセットアップ - name: Set up PHP uses: shivammathur/setup-php@v2 with: php-version: '8.1' extensions: mbstring, intl tools: composer # 依存インストール & テスト実行 - run: composer install --prefer-dist --no-progress --no-suggest - run: vendor/bin/phpunit # アセットビルド(npm が必要な場合) - name: Build assets working-directory: ./assets run: | npm ci npm run build # ZIP パッケージ作成 - name: Create ZIP run: | mkdir release zip -r release/modern-plugin.zip . -x "*.git*" "node_modules/*" "tests/*" # アーティファクトとして保存 - uses: actions/upload-artifact@v3 with: name: plugin-zip path: release/modern-plugin.zip |
このワークフローは **push → テスト → ビルド → ZIP 作成** を自動化し、生成された modern-plugin.zip がダウンロード可能になります。
---
## 7. 参考リンク(信頼性の高い公式情報)
- WordPress サーバー要件:
- プラグインヘッダーリファレンス:
- PHP 8.1 の新機能(公式マニュアル):
- Composer ドキュメント(PSR‑4 オートローディング):
- Gutenberg ブロック開発ガイド:
- WordPress Scripts パッケージ:
---
## まとめ
1. **ローカル環境**は XAMPP/LocalWP と PHP 8.1+、Composer を組み合わせるだけで即座に開発基盤が完成します。
2. **プラグイン雛形**は必須ヘッダーとディレクトリ構成のみで WordPress に認識させられます。
3. **名前空間 + PSR‑4**は Composer の autoload 設定で自動ロードが実現し、コードの保守性が向上します。
4. **基本機能(フック・ショートコード・CPT)**はクラスに切り出すことでテスト可能かつ再利用しやすくなります。
5. **アセット管理と Gutenberg 連携**は wp_enqueue_* と npm/webpack ビルドでモダンブロックを作成できます。
6. **デバッグ・テスト・CI/CD**は WP_DEBUG、PHPUnit、GitHub Actions の3本柱で品質を自動的に担保します。
以上の手順とベストプラクティスに従えば、最新の WordPress 6.x / PHP 8 系列環境下で **安全・拡張性の高いカスタムプラグイン** を迅速に開発・配布できるようになります。
?>