管理画面カスタマイズの記事一覧

管理画面のカスタマイズ

  • アクションフックとは
  • 投稿画面のカスタマイズ
  • カスタムフィールドへ格納
  • CSRF 対策
  • DB とカスタムフィールド

管理画面カスタマイズ

→この記事の続きを読む

アクションフック

  • add_action (フック, プラグイン)
  • フック:WP の処理の目印
  • プラグイン実行箇所指定

→この記事の続きを読む

簡単なプラグイン例

  • 管理画面に CSS 追加
  • admin_head フック
  • ハイライト表示

function focus_highlight() {
    ?>
<style type="text/css">
input:focus,textarea:focus{
    background-color: lightyellow;
}
</style>
	<?php
}
add_action( 'admin_head', 'focus_highlight' );

→この記事の続きを読む

カスタムフィールド

  • 投稿(ページ)に項目追加
  • add_meta_box(ID, タイトル, 関数, タイプ)
  • データを保存する関数も必要
  • add_action(‘save_post’, 関数)

→この記事の続きを読む

投稿画面にボックスを追加

function add_post_custombox() {
	$custom_key = "mykey" ;
	global $post;
	echo '<input type="text" name="' . $custom_key . '" value="' . esc_attr( get_post_meta( $post->ID, $custom_key, true )) . '" size="15" />' ;
}
add_meta_box( 'mykeybox', 'mybox', 'add_post_custombox', 'post' ) ;

→この記事の続きを読む

データ保存

  • 記事保存時に実行
  • add_action( ‘save_post’, 関数 )
  • フォーム作成とは別に必要

→この記事の続きを読む

save_post_custombox

	$keywords = esc_attr( $_POST[$custom_key] ) ;
	if ( "" == get_post_meta( $post_id, $custom_key )) {
		add_post_meta( $post_id, $custom_key, $keywords, true ) ;
	} else if ( $keywords != get_post_meta( $post_id, $custom_key )) {
		update_post_meta( $post_id, $custom_key, $keywords ) ;
	} else if ( "" == $keywords ) {
		delete_post_meta( $post_id, $custom_key ) ;
	}

→この記事の続きを読む

CSRF対策

  • 攻撃者が不正リンクを作成
  • 管理者自身が不正リンクを踏む
  • 意図しないデータ改変
  • ワンタイムチケット発行

→この記事の続きを読む

wp_create_nonce

  • ワンタイムトークン生成
  • wp_create_nonce(ソルト)
  • 正規の遷移かチェック
  • CSRF 対策

	echo '<input type="hidden" name="' . $noncename . '" id="' . $custom_key_noncename . '" value="' . wp_create_nonce( 'salt' ) . '" />' ;

→この記事の続きを読む

wp_verify_nonce

  • ワンタイムトークンの認証
  • wp_verify_nonce(POST、ソルト)
  • データ格納時にチェック

	if ( !wp_verify_nonce( $_POST[$noncename], 'salt' )) {
		return $post_id;
	}

→この記事の続きを読む

データベース構造

  • キーと値の組を保存
  • 値はデータ型無(テキスト)
  • MYSQL でのソートや検索は困難

→この記事の続きを読む

スクリプトソース

function add_post_custombox()
{
	$custom_key = "mykey" ;
	$noncename = $custom_key . "noncename" ;
	global $post;
	echo '<input type="hidden" name="' . $noncename . '" id="' . $noncename . '" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />' ;
	echo '<label class="screen-reader-text" for="' . $custom_key . '">' . $custom_key . '</label> ' ;
	echo '<input type="text" name="' . $custom_key . '" value="' . esc_attr( get_post_meta( $post->ID, $custom_key, true )) . '" size="15" />' ;
}
function save_post_custombox( $post_id )
{
	$custom_key = "mykey" ;
	$noncename = $custom_key . "noncename" ;
	if ( !wp_verify_nonce( $_POST[$noncename], plugin_basename(__FILE__) )) {
		return $post_id;
	}
	if ( !current_user_can( 'edit_post', $post_id )) {
		return $post_id;
	}
	$keywords = esc_attr( $_POST[$custom_key] ) ;
	if ( "" == get_post_meta( $post_id, $custom_key )) {
		add_post_meta( $post_id, $custom_key, $keywords, true ) ;
	} else if ( $keywords != get_post_meta( $post_id, $custom_key )) {
		update_post_meta( $post_id, $custom_key, $keywords ) ;
	} else if ( "" == $keywords ) {
		delete_post_meta( $post_id, $custom_key ) ;
	}
}
function admin_menu_custombox()
{
add_meta_box( 'mykeybox', 'mybox', 'add_post_custombox', 'post', 'normal', 'low' ) ;
}
add_action( 'admin_menu', 'admin_menu_custombox' ) ;
add_action( 'save_post', 'save_post_custombox' ) ;

→この記事の続きを読む