WPプラグイン開発入門 (2)仕様と雛形

まず初めに、何もしないプラグインを作ってみます。WordPress管理画面からプラグインと認識され、有効化・無効化・アンインストールができるようになることを目標とします。

PHPファイル

ハンドブックのPlugin Basicsには、以下のように説明されています。

To get started creating a new plugin, follow the steps below.

  1. Navigate to the WordPress installation’s wp-content directory.
  2. Open the plugins directory.
  3. Create a new directory and name it after the plugin (e.g. plugin-name).
  4. Open the new plugin’s directory.
  5. Create a new PHP file (it’s also good to name this file after your plugin, e.g. plugin-name.php).
https://developer.wordpress.org/plugins/plugin-basics/

新しいプラグインの作成を始めるには、以下の手順に従ってください。

  1. WordPressのwp-contentディレクトリに移動する。
  2. pluginsディレクトリを開く。
  3. 新しいディレクトリを作成し、プラグインの名前を付ける(例:plugin-name)。
  4. 新しいプラグインのディレクトリを開く。
  5. 新しい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.php

PHPDocの形式で書くと、以下のようになります。

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ファイルを作成しておきます。

関連サイト

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です