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

WordPress 6.4.3
広告WordPress・PHP を学べる本

Amazonのアソシエイトとして、WordPress functions.phpは適格販売により収入を得ています。

概要説明

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

プラグインインストール

Smart Custom Fields

4.2.2 のあと、長く更新されていない時期がありました。使う前に、WordPress.org のページで最近の更新を確認してください。

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

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' => '項目説明',
    ),
]);

SiteBeacon(サイトビーコン)

サイトが止まったら、すぐに気づける。

ホームページを 5 分ごとにチェックして、止まったとき・エラーが出たとき・SSL やドメインの期限が近いときに、メールでお知らせします。2 サイトまでは、ずっと無料で使えます。

SiteBeacon を見る サイトを直す相談をする

監視で気づいたあとの直す作業も、同じ担当がお受けします。