acf_allow_unfiltered_html()


WordPressの関数acf_allow_unfiltered_html()は、Advanced Custom Fields(ACF)プラグインでHTMLフィルタリングを許可するために使用されます。この関数を使うことで、ユーザーが入力したコンテンツがフィルタリングされずにそのまま保存されるようになります。

シンタックス

acf_allow_unfiltered_html();

例1: 投稿のコンテンツにHTMLを許可する

ACFフィールドで入力されたHTMLをフィルタリングせずに保存します。

<?php if( function_exists('acf_allow_unfiltered_html') ) { acf_allow_unfiltered_html(); } ?>

例2: ACFフィールドの値をフィルタリングせずに保存する

ACFのフィールドに入力されたHTMLコードをそのまま保存します。

<?php 
$value = get_field('your_acf_field'); 
if( function_exists('acf_allow_unfiltered_html') ) {
    acf_allow_unfiltered_html();
}
echo $value;
?>

例3: フィールドのHTMLを保存する前にフィルタを無効にする

特定のACFフィールドに対してHTMLフィルタリングを無効化します。

<?php 
if( function_exists('acf_allow_unfiltered_html') ) {
    acf_allow_unfiltered_html();
    update_field('your_acf_field', '<strong>強調</strong>されたテキスト');
}
?>
[/code>

<h3>例4: フィルタを無効にしてコンテンツを出力する</h3>

ACFフィールドのコンテンツをフィルタリングせずにそのまま表示します。

[code language="php"]
<?php 
if( function_exists('acf_allow_unfiltered_html') ) {
    acf_allow_unfiltered_html();
    the_field('your_acf_field');
}
?>

例5: ユーザーが入力したHTMLを保存する

ユーザーが入力したHTMLコードをフィルタリングせずに保存します。

<?php 
if( function_exists('acf_allow_unfiltered_html') ) {
    acf_allow_unfiltered_html();
    update_field('your_acf_field', '<a href="https://example.com">リンク</a>');
}
?>

注意点

  • acf_allow_unfiltered_html()関数を使用する場合、セキュリティリスクが発生する可能性があるため、信頼できるユーザーにのみこの機能を許可するようにしてください。
  • HTMLコードの保存を許可すると、XSS(クロスサイトスクリプティング)攻撃などのリスクがあります。