Tech & Tips WordPress コーディング

[WordPress]ACFのフィールドグループをPHPで管理する[WP10]

この記事は2年以上前ですので、内容が古い可能性があります

WP10は弊社が掲げる「WordPressを使った10倍速開発」です。
10倍とは、フルスクラッチに比べて10倍ということです。

この連載ではWP開発に長く携わっている弊社のWordPress開発ベストプラクティスを発信していきます。

第6回はACFのフィールドグループをPHPで管理する手法を紹介します。

ACFをコードで管理する意義

「Infrastructure as Code(コードによる環境の構成管理)」という言葉が登場してから久しいです。これはコードでインフラを管理することにより、環境差異の発生を抑えようとする試みです。

ACFはインフラではありません。しかし、同じシステムの開発者同士でも、「ローカル環境でフィールドの設定を画面上から行う」場面は多数あります。この時、ヒューマンエラーによって開発者ごとに異なる設定がされた場合、プログラムを走らせた結果も変化するというリスクがあります。

環境差異のリスクを極力無くすために、ACFのフィールドのPHP化は大いに役立ちます。

ACFでフィールドを作成する

テキスト、テキストエリア、チェックボックスでフィールドを作成してみました。

これをPHPで出力します。

PHPコードのエクスポート

[ カスタムフィールド > Tools ]を開きます。

今回は「WP10連載」というフィールドグループを選択します

コード化したいフィールドグループを選択し、「Generate PHP」ボタンをクリックします。

フィールドグループがコード化されました。これをコピー&ペーストでエディタに貼り付けましょう。

ACFフィールドのPHPコード

if ( function_exists( 'acf_add_local_field_group' ) ):

    acf_add_local_field_group( [
       'key'                   => 'group_5b91d4399810b',
       'title'                 => 'WP10連載',
       'fields'                => [
           [
               'key'               => 'field_5b91d44432139',
               'label'             => '連載タイトル',
               'name'              => 'title',
               'type'              => 'text',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               'default_value'     => '',
               'placeholder'       => '',
               'prepend'           => '',
               'append'            => '',
               'maxlength'         => '',
           ],
           [
               'key'               => 'field_5b91d45b3213a',
               'label'             => '本文',
               'name'              => 'body',
               'type'              => 'textarea',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               'default_value'     => '',
               'placeholder'       => '',
               'maxlength'         => '',
               'rows'              => '',
               'new_lines'         => '',
           ],
           [
               'key'               => 'field_5b91d4683213b',
               'label'             => '対象者',
               'name'              => 'reader',
               'type'              => 'checkbox',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               'choices'           => [
                   'junior' => '初心者',
                   'middle' => '中級者',
                   'senior' => '上級者',
               ],
               'allow_custom'      => 0,
               'default_value'     => [
               ],
               'layout'            => 'vertical',
               'toggle'            => 0,
               'return_format'     => 'value',
               'save_custom'       => 0,
           ],
       ],
       'location'              => [
           [
               [
                   'param'    => 'post_type',
                   'operator' => '==',
                   'value'    => 'post',
               ],
           ],
       ],
       'menu_order'            => 0,
       'position'              => 'normal',
       'style'                 => 'default',
       'label_placement'       => 'top',
       'instruction_placement' => 'label',
       'hide_on_screen'        => [
           0 => 'the_content',
       ],
       'active'                => 1,
       'description'           => '',
   ] );

endif;

連載タイトルフィールドの内容を解説します。

[
    'key' => 'field_5b91d44432139', // フィールドキー。各フィールドに一意にあたえられます
    'label' => '連載タイトル',        // フィールドラベル
    'name'  => 'title',            // フィールド名
    'type' => 'text',              // フィールドタイプ
    'instructions'      => '', // 投稿者向けの説明
    'required'          => 0,  // 必須項目か否か。0が非必須、1が必須
    'conditional_logic' => 0,  // 条件判定
    'wrapper'           => [   // ラッパーの属性
        'width' => '',
        'class' => '',
        'id'    => '',
    ],
    'default_value' => '',  // デフォルト値
    'placeholder' => '',    // プレースホルダーのテキスト
    'prepend' => '',        // 先頭に追加
    'append' => '',         // 末尾に追加
    'maxlength' => '',      // 制限文字数
],

また、フィールドの配列の下にある項目についてもコメントを加えます。

'location' => [  // 位置。どの編集画面でカスタムフィールドを表示するかを決定するルールを作成します
    [
        [
            'param'    => 'post_type',  // 投稿タイプ
            'operator' => '==',         // 等しい
            'value'    => 'post',       // 投稿
        ],
    ],
],
'menu_order'      => 0,              // 順番
'position'        => 'normal',       // 位置
'style'           => 'default',      // スタイル
'label_placement' => 'top',          // ラベルの配置
'instruction_placement' => 'label',  // 説明の配置
'hide_on_screen'        => [         // 画面に非表示
    0 => 'the_content',              // コンテンツエディタ
],
'active'      => 1,  // Activeである。「いいえ」は0、「はい」は1
'description' => '' // 説明

これで[カスタムフィールド > フィールドグループを編集]の設定値がコードで管理できるようになりました。

設定画面から見ることができない値は以下のキーです。

// グループキー
'key' => 'group_5b91d4399810b'
// '連載タイトル'のフィールドキー
'key' => 'field_5b91d44432139'
//  '本文',のフィールドキー
'key' => 'field_5b91d45b3213a',
// '対象者',のフィールドキー
'key' => 'field_5b91d4683213b',

これらはPlugin開発者は把握しておかなければならない事項です。ACFの関数は「フィールドキーまたはフィールド名」を引数に取るものが多数あるからです。フィールド名を頻繁に変更する場合、フィールド名を定数で管理するか、一意に決まるフィールドキーを引数に渡してあげましょう。

PHPコードのインポート

せっかくなのでフィールドをカスタマイズしてみましょう。変更箇所をコメントに記しています。

if ( function_exists( 'acf_add_local_field_group' ) ):

    // '対象者'フィールドの'choices'の中身を外出し
    $target = [
        '0' => '入門編',
        '1' => '初心者',
        '2' => '中級者',
        '3' => '上級者',
    ];

    acf_add_local_field_group( [
       'key'                   => 'group_5b91d4399810b',
       'title'                 => 'WP10連載',
       'fields'                => [
           [
               'key'               => 'field_5b91d44432139',
               // フィールドのタイトル'連載タイトル'を変更します
               'label'             => 'WP10連載記事タイトル',
               'name'              => 'title',
               'type'              => 'text',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               'default_value'     => '',
               'placeholder'       => '',
               'prepend'           => '',
               'append'            => '',
               'maxlength'         => '',
           ],
           [
               'key'               => 'field_5b91d45b3213a',
               'label'             => '本文',
               'name'              => 'body',
               'type'              => 'textarea',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               'default_value'     => '',
               // 説明を追加
               'placeholder'       => 'ここに本文を記入します',
               'maxlength'         => '',
               'rows'              => '',
               'new_lines'         => '',
           ],
           [
               'key'               => 'field_5b91d4683213b',
               'label'             => '対象者',
               // フィールドネームを'reader'から変更
               // (この変更は表側から見ることはできません)
               'name'              => 'target',
               'type'              => 'checkbox',
               'instructions'      => '',
               'required'          => 0,
               'conditional_logic' => 0,
               'wrapper'           => [
                   'width' => '',
                   'class' => '',
                   'id'    => '',
               ],
               // 配列を外出しして管理しやすくした
               'choices'           => $target,
               'allow_custom'      => 0,
               'default_value'     => [
               ],
               // verticalが垂直。horizontalが水平
               'layout'            => 'horizontal',
               'toggle'            => 0,
               'return_format'     => 'value',
               'save_custom'       => 0,
           ],
       ],
       'location'              => [
           [
               [
                   'param'    => 'post_type',
                   'operator' => '==',
                   'value'    => 'post',
               ],
           ],
       ],
       'menu_order'            => 0,
       'position'              => 'normal',
       'style'                 => 'default',
       'label_placement'       => 'top',
       'instruction_placement' => 'label',
       'hide_on_screen'        => [
           0 => 'the_content',
       ],
       'active'                => 1,
       'description'           => '',
   ] );

endif;

これをfunctions.phpに貼り付けます。すると、フィールドの変更が反映されました。

before
after

楽しいACFライフを!

この記事をシェアする:
◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇

私たちと一緒に働きませんか?

「[WordPress]ACFのフィールドグループをPHPで管理する[WP10]」はいかがでしたか?
株式会社プレスマンでは、 WordPressが大好きな方、仕事を通してさらにスキルを磨きたい方を募集しています。 まずは募集職種をご覧の上、お気軽にお問い合わせください。 あなたとお会いできるのを楽しみにしています。

採用情報を見る

-Tech & Tips, WordPress, コーディング
-,

© 2024 PRESSMAN*Tech Powered by STINGER