まず初めに、何もしないプラグインを作ってみます。WordPress管理画面からプラグインと認識され、有効化・無効化・アンインストールができるようになることを目標とします。
PHPファイル
ハンドブックのPlugin Basicsには、以下のように説明されています。
To get started creating a new plugin, follow the steps below.
https://developer.wordpress.org/plugins/plugin-basics/
- Navigate to the WordPress installation’s wp-content directory.
- Open the plugins directory.
- Create a new directory and name it after the plugin (e.g.
plugin-name
).- Open the new plugin’s directory.
- Create a new PHP file (it’s also good to name this file after your plugin, e.g.
plugin-name.php
).
新しいプラグインの作成を始めるには、以下の手順に従ってください。
- WordPressのwp-contentディレクトリに移動する。
- pluginsディレクトリを開く。
- 新しいディレクトリを作成し、プラグインの名前を付ける(例:plugin-name)。
- 新しいプラグインのディレクトリを開く。
- 新しいPHPファイルを作成する(このファイルにもplugin-name.phpなどプラグインの名前を付けるとよい)。
では、この手順に沿って開発を始めてみましょう。SSHで接続し、ターミナルで操作します。WordPressをインストールしたフォルダ(wp-contentがあるフォルダ)に移動し、以下を実行します。
Bash
$ cd wp-content
$ cd plugins
$ mkdir dashboard-right-now-plus
$ cd dashboard-right-now-plus
$ touch dashboard-right-now-plus.php
ヘッダーコメント
このdashboard-right-now-plus.php
ファイルに特定のコメントを書くことで、WordPressからプラグインとして認識されます。プラグイン名は必須です。それ以外にバージョン、作者情報、ライセンスなどを含めることができます。
最低限のヘッダーコメントは以下のとおりです。
PHP
/*
* Plugin Name: Dashboard Right Now Plus
*/
今回はもう少し詳しく書くことにします。以下のようになります。
PHP
<?php
/*
* Plugin Name: Dashboard Right Now Plus
* Plugin URI: https://gist.github.com/web83info/5747ac715e4544b408e91e2ec034ab05/
* Description: Add custom post type to "Right Now" meta box on WordPress dashboard
* Version: 1.0.0
* Requires at least: 6.4
* Requires PHP: 7.2
* Author: web83info
* Author URI: https://labs.web83.info/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
dashboard-right-now-plus.phpPHPDocの形式で書くと、以下のようになります。
PHP
/**
* Dashboard Right Now Plus
*
* @package DashboardRightNowPlus
*
* @wordpress-plugin
* Plugin Name: Dashboard Right Now Plus
* Plugin URI: https://gist.github.com/web83info/5747ac715e4544b408e91e2ec034ab05/
* Description: Add custom post type to "Right Now" meta box on WordPress dashboard
* Version: 1.0.0
* Requires at least: 6.4
* Requires PHP: 7.2
* Author: web83info
* Author URI: https://labs.web83.info/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
dashboard-right-now-plus.php管理画面から、プラグインの有効化・無効化・アンインストールができることを確認します。アンインストールしたら、もう一度dashboard-right-now-plus.php
ファイルを作成しておきます。