用語集プラグインの開発
当サイトの「記録抄」は用語集のようなコンテンツなのだが、現在はカスタム投稿タイプとテーマのカスタマイズの組み合わせで表示している。
これをテーマに依存しない、単独のプラグインに変更したいと思っている。せっかくなのでプラグイン化の作業記録も残していこうかと。初歩的なところから進めていくつもり。
プラグインヘッダーファイルの作成
まずはこちらを参考に、用語集プラグイン(仮称BK Simple Glossary)のプラグインヘッダーを作成した。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php /* * Plugin Name: BK Simple Glossary * Description: Adds a glossary feature to WordPress. * Version: 1.0 * Author: Bun Kitani * Author URI: https://genniroku.com/ * License: GPL v2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html */ if ( ! defined( 'ABSPATH' ) ) { exit; } |
管理メニューの追加
こういうのはまずは見た目から、ということでトップレベルメニューとサブメニューを追加する。利用する関数はadd_menu_page()とadd_submenu_page()だ。
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 | add_action( 'admin_menu', 'bk_simple_glossary_add_menu' ); function bk_simple_glossary_add_menu() { add_menu_page( 'BK Simple Glossary', 'BK Simple Glossary', 'manage_options', 'bk-simple-glossary-add', 'bk_simple_glossary_render_add_page' ); add_submenu_page( 'bk-simple-glossary-add', '新規の用語集を追加', '新規追加', 'manage_options', 'bk-simple-glossary-add' ); add_submenu_page( 'bk-simple-glossary-add', '用語集の編集と削除', '編集', 'manage_options', 'bk-simple-glossary-edit', 'bk_simple_glossary_render_edit_page' ); } function bk_simple_glossary_render_add_page() { require plugin_dir_path(__FILE__) . 'admin/add.php'; } function bk_simple_glossary_render_edit_page() { require plugin_dir_path(__FILE__) . 'admin/edit.php'; } |
用語集を登録する「新規追加」と、登録した用語集の編集・削除を行える「編集」のメニューを用意した。
サブメニューを追加した場合、サブメニューの1個目はadd_menu_page()で追加した親メニュー名になってしまう。このため最初のadd_submenu_page()で、サブメニューの1個目のメニュー名を上書きしている。参考にしたのはこちら。
クラス化
後々のことを考えてクラス化しておく。classesディレクトリを作成し、その中にadmin-menu.phpを作成する。
1 2 3 | $ mkdir classes $ cd classes $ vi admin-menu.php |
とりあえずさっきのコードをクラス化。
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 44 45 46 47 | <?php namespace BKSimpleGlossary\Admin; class Menu { private $plugin_dir; public function __construct($plugin_dir) { $this->plugin_dir = $plugin_dir; add_action( 'admin_menu', [$this, 'add_menu'] ); } public function add_menu() { add_menu_page( 'BK Simple Glossary', 'BK Simple Glossary', 'manage_options', 'bk-simple-glossary-add', [$this, 'render_add_page'] ); add_submenu_page( 'bk-simple-glossary-add', '新規の用語集を追加', '新規追加', 'manage_options', 'bk-simple-glossary-add', [$this, 'render_add_page'] ); add_submenu_page( 'bk-simple-glossary-add', '用語集の編集と削除', '編集', 'manage_options', 'bk-simple-glossary-edit', [$this, 'render_edit_page'] ); } public function render_add_page() { require $this->plugin_dir . 'admin/add.php'; } public function render_edit_page() { require $this->plugin_dir . 'admin/edit.php'; } } |
プラグイン本体側。Akismetのソースを参考に、バージョンとプラグインディレクトリは定数で定義しておく。
1 2 | define( 'BK_SIMPLE_GLOSSARY_VERSION', '1.0' ); define( 'BK_SIMPLE_GLOSSARY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
最初、下記のように$menuに突っ込んでたらエラーになった。グローバル変数で$menuがあるようで上書きしてしまっていたようだ。
1 | $menu = new \BKSimpleGlossary\Admin\Menu( BK_SIMPLE_GLOSSARY_PLUGIN_DIR ); |
ここまでのコードをAIに見せたら、初期化関数を用意してグローバル変数を汚染するリスクを減らした方が良いよ、と言われたのでそのようにした。インスタンスの変数名も変更。
1 2 3 4 5 6 7 | require_once BK_SIMPLE_GLOSSARY_PLUGIN_DIR. 'classes/admin-menu.php'; add_action('plugins_loaded', 'bk_simple_glossary_init'); function bk_simple_glossary_init() { $glossary_menu_instance = new \BKSimpleGlossary\Admin\Menu( BK_SIMPLE_GLOSSARY_PLUGIN_DIR ); } |
次回
add.phpとedit.phpの中身と登録処理かなぁ。