2017.12.25WEB - WordPress

Smart Custom Fieldsをfunctions.phpから追加する

Smart Custom Fieldsの便利な所。
smart-cf-register-fieldsフックを使ってfunctions.php等から呼び出せるので重宝しています。

例えば、特定の投稿IDにのみの利用の場合はデフォルトでも可能ですが、
特定の固定ページの子ページのみ(特定の親を持つ子ページのみ)等の設定までできてしまうし、
環境を移す際の反映も簡単にできてしまうわけです。

Smart Custom Fields でカスタムフィールドの定義を Git で管理できるようになりました!

記述

基本的な記述

function my_register_fields( $settings, $type, $id, $meta_type ) {
  $Setting = SCF::add_setting( 'カスタムフィールド用ユニークなID', 'メタボックスのタイトル' );
  $Setting->add_group( 'フィールドグループ用ユニークなID', 繰り返すかをtrueかfalseで, array(
    array(
      'name'  => 'ユニークなフィールドの名前',
      'label' => 'フィールドのラベル',
      'type'  => 'タイプ',
    ),
    array(
      'name'    => 'ユニークなフィールドの名前2',
      'label'   => 'フィールドのラベル2',
      'type'    => 'タイプ',
    ),
  )); 
  $settings[] = $Setting;
  return $settings;
}
add_filter( 'smart-cf-register-fields', 'my_register_fields', 10, 4 );

タイプ別の記述

タイプの箇所へは、テキスト、チェックボックス等の指定を記述します。

テキスト

    array(
      'type'        => 'text',                   // タイプ
      'name'        => 'scf-1',                  // 名前
      'label'       => 'テキストのテスト',       // ラベル
      'default'     => 'デフォルト値',           // デフォルト
      'instruction' => '説明文です。',           // 説明文
      'notes'       => 'ここに注記'              // 注記
    ),

真偽値

    array(
      'type'        => 'boolean',                // タイプ
      'name'        => 'scf-2',                  // 名前
      'label'       => '真偽値のテスト',         // ラベル
      'default'     => true,                     // デフォルト true or false
      'true_label'  => 'はい',                   // TRUE ラベル
      'false_label' => 'いいえ',                 // FALSE ラベル
      'instruction' => 'ここにテキスト',         // 説明文
      'notes'       => 'ここに注記',             // 注記
    ),

テキストエリア

    array(
      'type'        => 'textarea',               // タイプ
      'name'        => 'scf-3',                  // 名前
      'label'       => 'テキストエリアのテスト', // ラベル
      'default'     => 'デフォルトのテキスト',   // デフォルト
      'rows'        => 5,                        // 行数
      'instruction' => 'ここにテキスト',         // 説明文
      'notes'       => 'ここに注記',             // 注記
    ),

チェックボックス

    array(
      'type'     => 'check',                     // タイプ
      'name'     => 'scf-4',                     // 名前
      'label'    => 'チェックボックスのテスト',  // ラベル
      'choices'  => array(
        'key'    => 'value',
        'キー'   => '値',
        // 'ダブルアローがない場合', // エラーになります
        '選択肢1'  => '値1',
        '選択肢2'  => '値2',
        'apple'    => 'りんご',
        'banana'   => 'バナナ',
        // キー名がBoolean型の場合、ラベルがキーとなります
        true       => 'はい',   // 取り出すと true ではなく 'はい' となります
        false      => 'いいえ', // 取り出すと false ではなく 'いいえ' となります
      ),
      'check_direction' => 'horizontal',              // 表示方向 vertical or horizontal
      'default'         => array('apple', 'banana'),  // デフォルト値。連想配列キー名を入れます。
      'instruction'     => 'ここにテキスト',          // 説明文
      'notes'           => 'ここに注記',              // 注記
    ),

ラジオボタン

    array(
      'type'     => 'radio',                     // タイプ
      'name'     => 'scf-5',                     // 名前
      'label'    => 'ラジオボタンのテスト',      // ラベル
      'choices'  => array(
        'key'    => 'value',
        'キー'   => '値',
        // 'ダブルアローがない場合', // エラーになります
        '選択肢1'  => '値1',
        '選択肢2'  => '値2',
        'apple'    => 'りんご',
        'banana'   => 'バナナ',
      ),
      'check_direction' => 'horizontal',              // 表示方向 vertical or horizontal
      'default'         => 'apple',                   // デフォルト値。
      'instruction'     => 'ここにテキスト',          // 説明文
      'notes'           => 'ここに注記',              // 注記
    ),

セレクトボックス

    array(
      'type'     => 'select',                    // タイプ
      'name'     => 'scf-5',                     // 名前
      'label'    => 'ラジオボタンのテスト',      // ラベル
      'choices'  => array(
        'key'    => 'value',
        'キー'   => '値',
        // 'ダブルアローがない場合', // エラーになります
        '選択肢1'  => '値1',
        '選択肢2'  => '値2',
          'apple'  => 'りんご',
          'banana' => 'バナナ',
        ),
      'default'     => 'apple',                  // デフォルト値。
      'instruction' => 'ここにテキスト',         // 説明文
      'notes'       => 'ここに注記',             // 注記
    ),

ファイル

    array(
      'type'  => 'file',                         // タイプ
      'name'  => 'scf-7',                        // 名前
      'label' => 'ファイルのテスト',             // ラベル
      'notes' => 'ここに注記',                   // 注記
    ),

画像

    array(
      'type'  => 'image',                        // タイプ
      'name'  => 'scf-8',                        // 名前
      'size'  => 'full',                         // プレビューサイズ full, thumbnail, medium or large
      'label' => 'ファイルのテスト',             // ラベル
      'notes' => 'ここに注記',                   // 注記
    ),

WYSIWYGエディタ

    array(
      'type'    => 'wysiwyg',                    // タイプ
      'name'    => 'scf-9',                      // 名前
      'default' => 'デフォルトの内容',           // default
      'label'   => 'WYSIWYGエディタのテスト',    // ラベル
      'notes'   => 'ここに注記',                 // 注記
    ),

カラーピッカー

    array(
      'type'    => 'colorpicker',                // タイプ
      'name'    => 'scf-10',                     // 名前
      'default' => '#937cf9',                    // デフォルト値をRGBで
      'label'   => 'カラーピッカーのテスト',     // ラベル
      'notes'   => 'ここに注記',                 // 注記
    ),

デイトピッカー

    array(
      'type'        => 'datepicker',             // タイプ
      'name'        => 'scf-11',                 // 名前
      'default'     => '2018/01/01',             // デフォルト値
      'date_format' => 'yy/mm/dd',               // 日付のフォーマット。画面とデータベースの値の両方に影響します。
      'max_date'    => '+100y',                  // デイトピッカーで表示されるカレンダーの未来の最大値
      'min_date'    => '-100y',
      'label'       => 'ファイルのテスト'        // ラベル
      'notes'       => 'ここに注記',             // 注記
    ),

関連記事

    array(
      'type'      => 'relation',                 // タイプ
      'name'      => 'scf-12',                   // 名前
      'post-type' => array( 'post', 'page' ),    // 表示する投稿タイプ
      'label'       => 'ファイルのテスト'        // ラベル
      'notes'       => 'ここに注記',             // 注記
    ),

関連ターム

    array(
      'type'      => 'taxonomy',                 // タイプ
      'name'      => 'scf-13',                   // 名前
      'post-type' => array( 'category', 'tag' ), // 表示するタクソノミー
      'label'       => 'ファイルのテスト'        // ラベル
      'notes'       => 'ここに注記',             // 注記
    ),

例)特定の親を持つ子ページ

add_filter('smart-cf-register-fields', 'my_add_meta_box', 10, 4);
function my_add_meta_box($settings, $post_type, $post, $meta_type) {
  $my_post = false;
  switch($meta_type) {
    case 'post':
      $my_post = _hny_my_get_posts($post);
      break;
    case 'term':
      break;
  }
  if($my_post) {
    $settings = test_add_meta_box_test($settings, $post_type, $my_post, $meta_type);
  }
  return $settings;
}
function _hny_my_get_posts($my_post_id) {
  $args = array(
    'p' => $my_post_id,
    'post_type' => 'any'
  );
  $my_post = get_posts($args);
  if(is_array($my_post)) {
    $my_post = reset($my_post);
  }
  return $my_post;
}
 
function test_add_meta_box_test($settings, $post_type, $my_post, $meta_type) {
  if($meta_type == 'post' && $post_type == 'page' && $my_post->post_parent == 投稿ID) {
    $Setting = SCF::add_setting('カスタムフィールド用ユニークなID', 'メタボックスのタイトル');
    $Setting->add_group('フィールドグループ用ユニークなID', 繰り返すかをtrueかfalseで, array(
      array(
        'name'  => 'ユニークなフィールドの名前',
        'label' => 'フィールドのラベル',
        'type'  => 'タイプ',
      ),
      array(
        'name'    => 'ユニークなフィールドの名前2',
        'label'   => 'フィールドのラベル2',
        'type'    => 'タイプ',
      ),
    )); 
    $settings[] = $Setting;
    return $settings;
  }
  return $settings;
}
add_filter('smart-cf-register-fields', 'my_register_fields', 10, 4);

フィールドを増やしていく場合は、

$Setting->add_group

からを足していく。