プラグインの設定用の管理画面を作るときにコピペすると速いです。
全体
// 有効化時に実行する
function my_activation_function() {
// ここにアクティベーション時の処理
}
register_activation_hook(__FILE__, 'my_activation_function');
// 設定画面へのリンクを出し、設定画面表示の関数を指定する
function my_plugin_sub_page() {
add_submenu_page('親', 'ページタイトル', 'メニュータイトル', '権限', __FILE__, '設定画面表示用関数名');
}
add_action('admin_menu', 'my_plugin_sub_page');
// 設定画面を出す
function my_plugin_page() {
if ( $_POST['posted'] == 'yes' ) {
// 入力値を保存する処理
update_option('名前', '値');
}
?>
<div class="wrap">
<h2>タイトル</h2>
<form action="" method="post">
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="xxx">XXX</label></th>
<td><input type="" /></td>
</tr>
</table>
<input type="hidden" name="posted" value="yes" />
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存" /></p>
</form>
</div>
<?php
}
?>
親は、以下をよく使いそう。
- edit.php
- edit-comments.php
- themes.php
- plugins.php
- users.php / profile.php
- options-general.php
フォーム出力と保存の仕方
// 数値入力
<tr valign="top">
<th scope="row"><label for="xxx">XXX</label></th>
<td><input type="text" name="xxx" id="xxx" value="<?php echo get_option('xxx'); ?>" class="regular-text code" /></td>
</tr>
// 数値保存
update_option('xxx', intval($_POST['xxx']));
// テキストフォーム
<tr valign="top">
<th scope="row"><label for="xxx">XXX</label></th>
<td><input type="text" name="xxx" id="xxx" value="<?php echo get_option('xxx'); ?>" class="regular-text code" /></td>
</tr>
// テキスト保存
update_option('xxx', strip_tags(stripslashes($_POST['xxx'])));
// チェックボックス
<tr valign="top">
<th scope="row">XXX</th>
<td><input type="checkbox" name="xxx" id="xxx" value="1" <?php if(get_option('xxx')){ echo 'checked="checked"' } ?> /><label for="xxx">XXX</label></td>
</tr>
// チェックボックス保存
update_option('xxx', intval($_POST['xxx']));
// セレクトフォーム
<tr valign="top">
<th scope="row"><label for="xxx">XXX</label></th>
<td>
<select name="xxx" id="xxx">
<?php
$select_options = array(
array('value' => '送信値1', 'text'=>'表示値1'),
array('value' => '送信値2', 'text'=>'表示値2'),
array('value' => '送信値3', 'text'=>'表示値3')
);
foreach ($select_options as $select_option) : ?>
<option value="<?php echo esc_attr($select_option['value']); ?>" <?php if(get_option('abc') == $select_option['value']){ echo ' selected="selected"' } ?>><?php echo esc_attr($select_option['text']); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
// セレクト値保存
/* テキストなのか数字なのか */

コメント