_wp_kses_allow_pdf_objects()


WordPressの_wp_kses_allow_pdf_objects()関数は、PDFオブジェクトを許可するために使用されるフィルターフックです。この関数は、KSES(HTMLサニタイズライブラリ)がPDFオブジェクトを許可するように設定します。

構文

_wp_kses_allow_pdf_objects( array $allowed );
  • $allowed(array) — KSESによって許可されるHTML要素と属性の配列。

例1: PDFオブジェクトを許可する基本的な使用法

この例では、_wp_kses_allow_pdf_objects()を使用してPDFオブジェクトを許可します。

add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects');

例2: カスタムフィルターでPDFオブジェクトを許可

カスタムフィルターを使用して、特定の条件下でPDFオブジェクトを許可します。

function custom_allow_pdf($allowed) { return _wp_kses_allow_pdf_objects($allowed); } add_filter('wp_kses_allowed_html', 'custom_allow_pdf');

例3: 特定のユーザーロールに対してPDFオブジェクトを許可

管理者ユーザーのみにPDFオブジェクトを許可します。

if (current_user_can('manage_options')) { add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects'); }

例4: 特定の投稿タイプでPDFオブジェクトを許可

特定の投稿タイプ(例: ‘document’)でのみPDFオブジェクトを許可します。

if (get_post_type() == 'document') { add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects'); }

例5: ショートコード内でPDFオブジェクトを許可

ショートコード内でPDFオブジェクトを許可する例です。

function pdf_shortcode() { add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects'); return '<object data="example.pdf" type="application/pdf"></object>'; } add_shortcode('pdf', 'pdf_shortcode');

例6: 特定のページでPDFオブジェクトを許可

特定のページIDを持つページでのみPDFオブジェクトを許可します。

if (is_page(get_the_ID())) { add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects'); }

例7: カスタムフィールドに基づいてPDFオブジェクトを許可

カスタムフィールドの値に基づいてPDFオブジェクトを許可します。

if (get_post_meta(get_the_ID(), 'allow_pdf', true)) { add_filter('wp_kses_allowed_html', '_wp_kses_allow_pdf_objects'); }

注意点

  • この関数はセキュリティリスクを伴う可能性があります。信頼できないソースからのPDFオブジェクトを許可しないでください。
  • KSESのデフォルト設定を変更するため、他のプラグインやテーマとの互換性に注意が必要です。

関連機能: