[WP Plugin Dev] Settings API を利用した Custom Admin Menu の作り方
Settings API を利用するとフォームの作成で楽ができそうなので調べました。
[markdown]
## Settings API
こちらで「WordPressのディフォルトのユーザーインターフェイスと親和性の高い管理画面を構築することができます。」と紹介されていました。
売り上げランキング: 74,203
著者さまのブログのフォロー記事と Codex の情報。
> * [Settings API – WordPress Codex 日本語版](http://wpdocs.osdn.jp/Settings_API)
> * [WordPressの管理画面に独自のオプション保存をするためのSettings APIの使い方 – Shinichi Nishikawa’s](https://nskw-style.com/2014/wordpress/settings-api.html)
>
> Settings APIは、テーマやプラグインから管理画面に独自の設定保存領域を作るためのAPIで、バリデーションや保存などの大部分をWordPressに任せることができる便利なものです。
なるほど、ということで Custom Admin Menu を作成して試そうと考えたのですが、イマイチどのようなコードを書けば良いのかイメージできませんでした。
Google 先生にたずねたところ、こんなジェネレータを発見。
参考にさせて頂きました。
> * [WP Settings API | Generate your own settings page!](http://wpsettingsapi.jeroensormani.com/)
## サンプルの概要
ジェネレータのコードでうまく動かないところや変更点も含めたメモ。
– 「ダッシュボード>設定>deadwood2」に Custom Admin Menu を表示します。
– `div.wrap` を追加、 `h1` を移動しました。
– チェックボックス2つ。
– `null` で `Notice: Undefined index` が発生するので、[isset](http://php.net/manual/ja/function.isset.php) でチェックしています。
– [checked()](https://codex.wordpress.org/Function_Reference/checked) のシンタックスがうまく動かないよう(`$checked` と `$current` が逆?)。[Settings API](http://wpdocs.osdn.jp/Settings_API) のサンプルコードに寄せました。
– [add_settings_field()](https://developer.wordpress.org/reference/functions/add_settings_field/) に引数をとって、render 関数をまとめました。
– データは `wp_options` にこのような形式で入っていました。
“`prettyprinted
option_id option_name option_value autoload
1708 dw2_settings a:2:{s:20:”dw2_checkbox_field_0″;s:1:”1″;s:20:”dw2_checkbox_field_1″;s:1:”1″;} yes
“`
## サンプルコード
チェックボックスをいっぱい作れるようになりましたね。:-)
“`php
‘dw2_checkbox_field_0’,
‘label’ => ‘label 0’,
)
);
add_settings_field(
‘dw2_checkbox_field_1’,
__( ‘Settings field description 1’, ‘deadwood2’ ),
‘dw2_checkbox_field_render’,
‘pluginPage’,
‘dw2_pluginPage_section’,
array(
‘field_name’ => ‘dw2_checkbox_field_1’,
‘label’ => ‘label 1’,
)
);
}
// ——————————————————————
// The callback functions for rendering
// ——————————————————————
function dw2_checkbox_field_render( $args ) {
$options = get_option( ‘dw2_settings’ );
$field_name = $args[‘field_name’];
$label = $args[‘label’];
if ( isset( $options[ $field_name ] ) ) {
$current = $options[ $field_name ];
} else {
$current = ”;
}
echo ‘‘ . $label;
}
function dw2_settings_section_callback() {
echo __( ‘This section description’, ‘deadwood2’ );
}
// ——————————————————————
// The callback function for admin menu page
// ——————————————————————
function dw2_options_page() {
?>
deadwood2
* [Function Reference/add options page « WordPress Codex](https://codex.wordpress.org/Function_Reference/add_options_page)
> * [Function Reference/register setting « WordPress Codex](https://codex.wordpress.org/Function_Reference/register_setting)
> * [Function Reference/add settings section « WordPress Codex](https://codex.wordpress.org/Function_Reference/add_settings_section)
> * [Function Reference/add settings field « WordPress Codex](https://codex.wordpress.org/Function_Reference/add_settings_field)
> * [get_option() | Function | WordPress Developer Resources](https://developer.wordpress.org/reference/functions/get_option/)
> * [Function Reference/settings fields « WordPress Codex](https://codex.wordpress.org/Function_Reference/settings_fields)
> * [Function Reference/do settings fields « WordPress Codex](https://codex.wordpress.org/Function_Reference/do_settings_fields)
[/markdown]