自作のプラグインに独自の設定画面を追加する (WordPress関数をあまり使わない場合)
自分の作成したプラグインに独自の設定画面を追加したい場合のサンプルです。
完成例
(通常時)
(入力エラー)
(設定完了)
コード
※ プラグインの作成についてははじめてのプラグイン作成などを参考にして下さい。
※ 下記のサンプルはWordPressの標準で用意されている設定画面用の関数をあまり使用しない形になっています。
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<?php /* Plugin Name: - テスト設定画面プラグイン(シンプル) Plugin URI: http://example.com/ Description: テスト設定画面のプラグインです。WordPressで用意された関数をあまり使わないバージョンです。 Version: 1.0 Author: Test Author URI: http://example.com/ */ /** * テスト設定用のクラスです。 */ class TestSettingsPageSimple { /** 設定値 */ private $options; /** 成功メッセージ */ private $success; /** エラーメッセージ */ private $errors; /** * 初期化処理です。 */ public function __construct() { // メニューを追加します。 add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); // ページの初期化を行います。 add_action( 'admin_init', array( $this, 'page_init' ) ); } /** * メニューを追加します。 */ public function add_plugin_page() { // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); // $page_title: 設定ページの<title>部分 // $menu_title: メニュー名 // $capability: 権限 ( 'manage_options' や 'administrator' など) // $menu_slug : メニューのslug // $function : 設定ページの出力を行う関数 // $icon_url : メニューに表示するアイコン // $position : メニューの位置 ( 1 や 99 など ) add_menu_page( 'テスト設定', 'テスト設定', 'manage_options', 'test_setting', array( $this, 'create_admin_page' ) ); // 設定のサブメニューとしてメニューを追加する場合は下記のような形にします。 //add_options_page( 'テスト設定', 'テスト設定', 'manage_options', 'test_setting', array( $this, 'create_admin_page' ) ); } /** * 設定ページの初期化を行います。 */ public function page_init() { // DBの設定値を取得します。 $this->options = get_option( 'test_setting' ); $this->errors = array(); $this->success = ""; // データが送信されている場合 if ( isset($_POST['test_setting']) ) { $input = $_POST['test_setting']; // メッセージがない場合エラーを追加 $this->options['message'] = trim($input['message']); if( ! isset( $this->options['message'] ) || $this->options['message'] === '' ) { $this->errors['message'] = "メッセージを入力して下さい。"; } // 以下、項目を増やしたい場合は上記のような形で値のチェック等を行います。 // エラーがない場合保存処理 if ( ! $this->errors ) { update_option( 'test_setting', $this->options ); $this->success = "設定を保存しました。"; } } } /** * 設定ページのHTMLを出力します。 */ public function create_admin_page() { $message = isset( $this->options['message'] ) ? $this->options['message'] : ''; ?> <div class="wrap"> <h2>テスト設定</h2> <form method="post"> <?php // 成功メッセージ表示 if ( $this->success ) { ?> <div class="updated"><p><strong><?php esc_html_e($this->success) ?></strong></p></div> <?php } ?> <?php // エラーメッセージ表示(複数項目でも対応可にしたコードです) if ( $this->errors ) { ?> <?php foreach ($this->errors as $err) { ?> <div class="error"><p><strong><?php esc_html_e($err) ?></strong></p></div> <?php } ?> <?php } ?> <table class="form-table"> <tr> <th>メッセージ</th> <td> <input type="text" id="message" name="test_setting[message]" value="<?php esc_attr_e( $message ) ?>" /> </td> </tr> </table> <?php // 送信ボタンを出力します。 submit_button(); ?> </form> </div> <?php } } // 管理画面を表示している場合のみ実行します。 if( is_admin() ) { $test_settings_page_simple = new TestSettingsPageSimple(); } |