Skip to content

Instantly share code, notes, and snippets.

@kurudrive
Last active January 16, 2023 06:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurudrive/09f37db0939d57df08c1 to your computer and use it in GitHub Desktop.
Save kurudrive/09f37db0939d57df08c1 to your computer and use it in GitHub Desktop.
[WordPress]よく使うエスケープ処理の書き方
/* 参照・参考
http://firegoby.jp/archives/2244
http://www.rescuework.jp/blog/customfield-escape.html
*/
// HTMLをエスケープ
esc_html($content);
// URLをエスケープ
esc_url($url);
// 属性をエスケープ
esc_attr($attr);
// WordPressによるPタグ自動マークアップを使う場合
// ※エスケープ処理されているかどうか未確認
wpautop($content);
// テキストエリアで改行を有効
nl2br(esc_textarea($content));
// WordPressのthe_contentと同じ処理
apply_filters('the_content', $content );
// WordPress本体のコメントで許可されているHTMLと同じ条件でフィルターがかけられます。
wp_kses_data($content);
// 一方でリスクがあるHTML以外はなるべく使わせてあげたいけど、ユーザーは信用できない場合には、以下のようなフィルターを通すとJavaScriptやiframeなどが取り除かれます。
wp_kses_post($content);
// 特定のダグのみ許可
$allowed_html = array(
'a' => array(
'href' => array(),
'title' => array()
),
'br' => array(),
'em' => array(),
'strong' => array()
);
wp_kses($content,$allowed_html, $allowed_protocols);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment