WordPress のプラグイン構造は、
「1つの独立したフォルダ内に、PHPファイルを中心とした機能モジュールをまとめたもの」です。
以下では、公式ドキュメントを踏まえながら、基本構造と役割を解説します。
基本構成の例
WordPress のプラグインのフォルダ構成の例(プラグインの内容により異なる):
/wp-content/
└── plugins/
└── my-plugin/
├── my-plugin.php
├── readme.txt
├── uninstall.php
├── includes/
│ ├── class-my-plugin-admin.php
│ └── class-my-plugin-public.php
├── assets/
│ ├── css/
│ └── js/
└── languages/
└── my-plugin-ja.mo主なファイルの役割の例
| ファイル/フォルダ | 役割 |
|---|---|
my-plugin.php(任意) | プラグインのメインファイル。ヘッダー情報と初期化コードを記述 |
readme.txt | WordPress.org配布用の説明書(GitHub公開時も推奨) |
uninstall.php | プラグイン削除時の後処理(DB削除など) |
includes/ | クラス定義・ロジックなどを整理 |
assets/ | CSS・JS・画像など静的ファイルを格納 |
languages/ | 翻訳ファイル(.po / .mo)を格納 |
プラグインヘッダー(必須)
プラグインの識別は「メインPHPファイルのヘッダーコメント」で行います。
最小の必須項目)
<?php
/*
* Plugin Name: YOUR PLUGIN NAME
*/記載例)
<?php
/**
* Plugin Name: My Plugin
* Plugin URI: https://example.com/my-plugin
* Description: シンプルなプラグインの説明。
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Your Name
* Author URI: https://example.com
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-plugin
* Domain Path: /languages
*/WordPressはこのコメントを読み取り、管理画面の「プラグイン一覧」に表示します。
ファイル名やフォルダ名が違っても、このヘッダーがあれば有効化できます。
WordPress Developer Resources [Header Requirements – Plugin Handbook]
https://developer.wordpress.org/plugins/plugin-basics/header-requirements/
フック(Hooks)を利用した動作登録
WordPressプラグインは「フック」を使ってWordPress本体の処理に割り込みます。
代表的なフックは次の2種類です:
| 種類 | 説明 | 例 |
|---|---|---|
アクションフック(add_action) | 特定のタイミングで独自処理を追加 | add_action('init', 'my_plugin_init'); |
フィルターフック(add_filter) | データを加工して返す | add_filter('the_content', 'my_plugin_filter_content'); |
例)イメージ
add_action('init', function() {
// 初期化処理
});
add_filter('the_content', function($content) {
return $content . '<p>Thanks for reading!</p>';
});クラス構成の例
大規模プラグインでは「クラス」でロジックを分離します。
例)イメージ
// includes/class-my-plugin.php
class My_Plugin {
public function __construct() {
add_action('init', [$this, 'init']);
}
public function init() {
// 初期化コード
}
}
// メインファイルで読み込み
require_once plugin_dir_path(__FILE__) . 'includes/class-my-plugin.php';
new My_Plugin();アセットの読み込み(CSS・JS)
WordPress固有の関数 wp_enqueue_script() / wp_enqueue_style() を使用します。
例)イメージ
function my_plugin_enqueue_assets() {
wp_enqueue_style(
'my-plugin-style',
plugin_dir_url(__FILE__) . 'assets/css/style.css'
);
wp_enqueue_script(
'my-plugin-script',
plugin_dir_url(__FILE__) . 'assets/js/script.js',
['jquery'],
null,
true
);
}
add_action('wp_enqueue_scripts', 'my_plugin_enqueue_assets');アンインストール処理
プラグイン削除時にデータをクリーンアップしたい場合はuninstall.php を配置して次のように記述します:
例)イメージ
<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
//アンインストール処理など(DB/cron/ファイル等)
delete_option( 'my_plugin_option' );参考
WordPress開発者向けリソース アンインストール方法
https://developer.wordpress.org/plugins/plugin-basics/uninstall-methods
ライセンス表記
WordPress.org の公式ディレクトリに登録するプラグインは、GPL または GPL 互換ライセンスで配布することが必須です。WordPress 本体が GPL ライセンスであることから、プラグインも WordPress の派生物と見なされる可能性があり、GPL 互換ライセンスを採用することが推奨されています。
一方で厳密には、派生する外部サーバーで動作するサービス部分(SaaSやAPI)など一定の要件を満たす場合、プラグインに関わる全ての部分が必ずしも GPL 互換が必要ではないケースもあります。
実際に商用サービスとして成立している有名プラグインが、どのような構造で「許可される形」を取っているかについて、下記の記事で解説しています↓
有名WordPressプラグインの商用モデルを徹底解説|GPLライセンス 構造と収益モデル(Jetpack・Akismet・WooCommerce・ Yoast SEO・Elementor・WP Rocket)

一般的には以下のように明記します:
/**
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/1. プラグインはGNU一般公衆利用許諾書と互換性がなければならない
引用元:WordPress.org:https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/#1-plugins-must-be-compatible-with-the-gnu-general-public-license
GPL互換ライセンスであればどれでも構いませんが、WordPressと同じライセンス(「GPLv2以降」)の使用を強く推奨します。WordPress.orgでホストされているプラグインディレクトリに保存されているすべてのコード、データ、画像は、GPLまたはGPL互換ライセンスに準拠している必要があります。含まれるサードパーティ製のライブラリ、コード、画像などは、GPL互換である必要があります。
参考
GPL compatibility guidelines – WordPress.org
https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/#1-plugins-must-be-compatible-with-the-gpl
まとめ:最小限の動くプラグイン例
<?php
/**
* Plugin Name: Hello AI Plugin
* Description: 簡単な挨拶を出力するプラグイン。
* Version: 1.0.0
* Author: ai0w
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
add_action('wp_footer', function() {
echo '<p style="text-align:center;">Hello from AI Plugin!</p>';
});
この1ファイルを /wp-content/plugins/hello-ai-plugin/hello-ai-plugin.php に置くと、
「有効化」ボタンで動作します。有効化すると、フッターに文字列「Hello from AI Plugin!」が表示されます。
WordPress>プラグイン>インストール済みプラグイン

関連記事
実際にWordPressで商用サービスとして成立している有名プラグインが、どのような構造で「許可される形」を取っているかについて、下記の記事で解説しています↓

公式ドキュメント
- WordPress Developer Resources – Plugin Basics
https://developer.wordpress.org/plugins/plugin-basics/ - GPL compatibility guidelines – WordPress.org
https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/#1-plugins-must-be-compatible-with-the-gpl
コメント