LoginSignup
34
28

More than 3 years have passed since last update.

SmartCustomFieldsのコードでの定義についてのまとめ

Last updated at Posted at 2017-01-14

はじめに

WordPressのプラグイン、Smart Custom Fields (SCF)をコードで定義する方法についてです。
SCFはWordPressのカスタムフィールド用のプラグインで、繰り返し項目がデフォルトで使うことができます。
カスタムフィールドのコード定義についてのまとめです。

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

呼び出し

コードの定義の呼び出しはsmart-cf-register-fieldsのフィルタフックで行います。 

function.php
function my_register_fields( $settings, $type, $id, $meta_type ) {
 $Setting = SCF::add_setting( 'id-1', 'functions.php から追加 その1' );
 $items = array();  
 $Setting->add_group( 'group-name-1', false, $items ); 
}

add_filter( 'smart-cf-register-fields', 'my_register_fields', 10, 4 );

ここの$itemsを作ることでカスタムフィールドの定義が作られていきます。

// $Setting->add_group( 'ユニークなID', 繰り返し可能か, カスタムフィールドの配列 );
$items = array(
  array(
    'name'  => 'field-1',
    'label' => 'テストフィールド',
    'type'  => 'text',
  ),
);
  $Setting->add_group( 'front-page-slider', false, $items);

タイプ

テキスト

Screen Shot 2017-01-07 at 16.17.35.png

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

真偽値

Screen Shot 2017-01-07 at 16.18.14.png

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

テキストエリア

Screen Shot 2017-01-07 at 16.18.20.png

    array(
        'type'        => 'textarea',                      // タイプ
        'name'        => 'scf-3',                         // 名前
        'label'       => 'テキストエリアのテストです',        // ラベル
        'default'     => 'デフォルトのテキストです',          // デフォルト
        'rows'        => 5,                               // 行数
        'instruction' => 'ここにテキストを入力してください。', // 説明文
        'notes'       => 'ここに注記を入れてください',        // 注記
    ),

チェックボックス

Screen Shot 2017-01-07 at 16.18.26.png

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'           => 'ここに注記を入れてください',         // 注記
    ),

ラジオボタン

Screen Shot 2017-01-07 at 16.18.35.png

    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'           => 'ここに注記を入れてください',        // 注記
    ),

セレクトボックス

Screen Shot 2017-01-07 at 16.18.40.png

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

ファイル

Screen Shot 2017-01-07 at 16.18.46.png

    array(
        'type'  => 'file',                       // タイプ
        'name'  => 'scf-7',                      // 名前
        'label' => 'ファイルのテストです',          // ラベル
        'notes' => 'ここに注記を入れてください',    // 注記
    )

画像 

Screen Shot 2017-01-07 at 16.18.51.png

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

WYSIWYGエディタ

Screen Shot 2017-01-07 at 16.18.58.png

    array(
        'type'    => 'wysiwyg',                           // タイプ
        'name'    => 'scf-9',                            // 名前
        'default' => '<blink>デフォルトの内容です</blink>', // default
        'label'   => 'WYSIWYGエディタのテストです',         // ラベル
        'notes'   => 'ここに注記を入れてください',           // 注記
    ),

カラーピッカー

Screen Shot 2017-01-07 at 16.19.04.png

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

デイトピッカー

Screen Shot 2017-01-07 at 16.19.10.png

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

関連記事

Screen Shot 2017-01-07 at 16.19.16.png

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

関連ターム

Screen Shot 2017-01-07 at 16.19.21.png

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

呼び出し条件

戻り値

フック

34
28
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
28