acf_get_checkbox_input()


WordPressのacf_get_checkbox_input()関数は、Advanced Custom Fieldsプラグインで使用され、チェックボックスフィールドの入力HTMLを取得します。

構文

acf_get_checkbox_input( array $args );

引数の説明:

  • $args (array) — 入力要素の設定を指定する連想配列。
  • 'name' (string) — フィールド名。
  • 'value' (string|array) — 選択された値。
  • 'choices' (array) — 選択肢のリスト。
  • 'class' (string) — カスタムCSSクラス。
  • 'disabled' (bool) — フィールドを無効化する場合はtrue

例 1. 単純なチェックボックスフィールドの出力

<?php
echo acf_get_checkbox_input( array(
    'name' => 'example_field',
    'choices' => array( 'option_1' => 'Option 1', 'option_2' => 'Option 2' ),
    'value' => 'option_1',
) );
?>

例 2. 複数選択可能なチェックボックス

<?php
echo acf_get_checkbox_input( array(
    'name' => 'multi_select_field',
    'choices' => array( 'opt_1' => 'Option 1', 'opt_2' => 'Option 2', 'opt_3' => 'Option 3' ),
    'value' => array( 'opt_1', 'opt_3' ),
) );
?>

例 3. カスタムクラスを追加

<?php
echo acf_get_checkbox_input( array(
    'name' => 'styled_field',
    'choices' => array( 'yes' => 'Yes', 'no' => 'No' ),
    'class' => 'custom-class',
) );
?>

例 4. 無効化されたチェックボックスフィールド

<?php
echo acf_get_checkbox_input( array(
    'name' => 'disabled_field',
    'choices' => array( 'enable' => 'Enable', 'disable' => 'Disable' ),
    'value' => 'disable',
    'disabled' => true,
) );
?>

例 5. 動的な選択肢を生成

<?php
$dynamic_choices = array(
    'key_1' => 'Dynamic Option 1',
    'key_2' => 'Dynamic Option 2',
);

echo acf_get_checkbox_input( array(
    'name' => 'dynamic_field',
    'choices' => $dynamic_choices,
) );
?>

注意事項

  • $args'choices'キーは必須で、選択肢を適切に指定する必要があります。
  • 出力されたHTMLを安全に処理するために、サニタイズ関数を使用することを推奨します。