http://www.webopixel.net/wordpress/631.html
例: 管理画面で入力したテキストを表示するプラグイン
[wp-content] – [plugins] – [show-text] –
show-text.php
—-
<?php
/*
Plugin Name: Show Text
Plugin URI: http;//www.example.com/plugin
Description: テキストを表示するだけのプラグイン
Author: my name
Version: 0.1
Author URI: http://www.example.com
*/
class ShowText {
public function __construct() {
// オプション画面に入力メニューを作成。
// 'admin_menu' にクラス $this の function'add_pages'を渡している。
add_action( 'admin_menu', array($this, 'add_pages') );
}
// トップレベルのメニューに追加 'add_menu_page'
// "public" でなければ機能しない。
public function add_pages() {
// (サブメニューに追加するなら 'add_submenu_page')
// 引数: ページタイトル、管理画面タイトル、ユーザ権限、メニューページに表示するために呼び出すPHPファイル(ここでは自身)、メニューページに表示するために呼び出す関数、これは空、メニューの場所
add_menu_page( 'テキスト設定', 'テキスト設定', 'level_8', __FILE__, array($this, 'show_text_option_page'), '', 26 );
}
// 管理画面に表示する内容。
// "public" でなければ機能しない。
function show_text_option_page() {
// $_POST[‘showtext_options’] があったら保存。
if ( isset($_POST[‘showtext_options’]) ) {
// If it can't valid the nonce, soon show error and die.
// check_admin_referer で CSRF もチェック.
check_admin_referer('add_title', 'swoptions');
$opt = $_POST[‘showtext_options’];
// wp_options に保存。
update_option('showtext_options', $opt);
?>
<!– セーブ完了。 –>
<div class="update fade"><p><strong><?php _e('Options saved.'); ?></strong></p></div>
<?php
}
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br /></div>
<h2>テキスト設定</h2>
<!– action="" –>
<form action="" method="post">
<?php
wp_nonce_field( 'add_title' 'swoptions'); // nonce を設定。
$opt = get_option('showtext_options');
$show_text = isset($opt[‘text’]) ? $opt[‘text’] : null;
?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="inputtext">テキスト</label></th>
<!– input の name は配列として(クオーテーションは必要ありません。) –>
<td><input name="showtext_options[text]" type="text" id="inputtext" value="<?php echo $show_text ?>" class="regular-text" /></td>
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" class="button-primary" value="変更を保存" /></p>
</form>
<!– /.wrap –></div>
<?php
}
// テンプレートに表示するためのメソッド
// 使い方: テンプレートに echo esc_html($showtext->get_text());
// これも public でなければ機能しない。
public function get_text() {
$opt = get_option('showtext_options');
return isset($opt[‘text’]) ? $opt[‘text’] : null;
}
}
$stext = new ShowText;