Smart Custom Fieldsの定義をfunctions.phpで行う

概要説明

Smart Custom Fieldsを使う時にfunctions.phpにてカスタムフィールドを行うときのカスタマイズ。画面からカスタムフィールドの定義は行えるのですがfunctions.phpでも定義できますという内容です。

バージョン:4.2.2から更新が止まっているようなので使用の際には要注意です。

管理画面からカスタムフィールド追加

Smart Custom Fieldsの定義をfunctions.phpで行う

functions.phpにてカスタムフィールド追加

functions.php
/*----------------------------------------------------
 Smart Custom Fields定義
----------------------------------------------------*/
add_filter('smart-cf-register-fields', function ($settings, $post_type, $id, $meta_type) {

    // 固定ページのみに制限
    if ( !in_array( $post_type, array( 'page' ) ) ) {
        return $settings;
    }

    $Setting = SCF::add_setting( 'sample_area', 'サンプルエリア' );

    // テキスト項目
    $Setting->add_group( 'scf_text', false, array(
        array(
            'name'    => 'text',
            'label'   => 'テキスト項目',
            'type'    => 'text',
            'default' => '初期値',
            'instruction' => '項目説明',
        ),
    ) );

    $settings[] = $Setting;

    return $settings;
}, 10, 4);

テキストエリア項目追加

functions.php
// テキストエリア
$Setting->add_group( 'scf_textarea', false, array(
    array(
        'name'    => 'textarea',
        'label'   => 'テキストエリア',
        'type'    => 'textarea',
        'rows'    => 2, // 表示行数
        'default' => '初期値',
        'instruction' => '項目説明',
    ),
) );

画像アップロード項目追加

functions.php
// 画像
$Setting->add_group('scf_image', true, [
    array(
        'name'  => 'image',
        'label' => '画像',
        'type'  => 'image',
        'size'  => 'thumbnail',
        'instruction' => '項目説明',
    ),
]);

ファイルアップロード項目追加

functions.php
// ファイル
$Setting->add_group('scf_file', true, [
    array(
        'name'  => 'file',
        'label' => 'ファイル',
        'type'  => 'file',
        'instruction' => '項目説明',
    ),
]);

セレクトボックス項目追加

functions.php
// セレクトボックス
$Setting->add_group('scf_select', true, [
    array(
        'name'  => 'select',
        'label' => 'セレクトボックス',
        'type'  => 'select',
        'choices' => [
            '0'  => '選択肢A',
            '1'  => '選択肢B',
        ],
        'instruction' => '項目説明',
    ),
]);

チェックボックス項目追加

functions.php
// チェックボックス
$Setting->add_group('scf_check', true, [
    array(
        'name'  => 'check',
        'label' => 'チェックボックス',
        'type'  => 'check',
        'choices' => [
            '0'  => '選択肢A',
            '1'  => '選択肢B',
        ],
        'instruction' => '項目説明',
    ),
]);

ラジオボタン項目追加

functions.php
// ラジオボタン
$Setting->add_group('scf_radio', true, [
    array(
        'name'  => 'radio',
        'label' => 'ラジオボタン',
        'type'  => 'radio',
        'choices' => [
            '0'  => '選択肢A',
            '1'  => '選択肢B',
        ],
        'instruction' => '項目説明',
    ),
]);

関連機能

  1. 投稿ページ以外でタグ機能を有効化

  2. RSSフィードを使ってWordPress同士のコンテンツを共有

  3. 指定期間内に公開した記事タイトルに一括で新着表示をつける

サイドバー

よく使うカスタマイズ

最近の記事

アニメの名言集

諦めないのが オレの魔法だ!!!!

ブラッククローバー
by アスタ

Profile

PAGE TOP
Amazon プライム対象