Guide to registering ACF fields via PHP

Par Alan Pilloud
Publié le 31 août 2017

Registering ACF fields via PHP offers some interresting advantages - like easier deploys, fewer DB hits, usable in versioning systems etc.

Here is a guide of available ACF fields for you to include in your ACF field groups.

If you don’t know how to register fields via PHP, read Register fields via PHP.

Minimal field

Remember that only key, label, name and type keys are required.

You could write a simple field like this :

[
    'key' => 'field_5981dad47e18f',
    'name' => 'text',
    'label' => 'My text field',
    'type' => 'text',
],

Key and name

This guide shows fields that have been exported directly from ACF visual editor. It creates fields with a key being a unique identifier.

To write fields faster without having to think too much, we follow this naming convention :

[
    'key' => 'field_[group_name]_[field_name]',
    'name' => '[group_name]_[field_name]',
    ...
],

Fields

Basic

Text

Official documentation : https://www.advancedcustomfields.com/resources/text

[
    'key' => 'field_5981dad47e18f',
    'label' => 'test',
    'name' => 'test',
    'type' => 'text',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'placeholder' => '',
    'prepend' => '',
    'append' => '',
    'maxlength' => '',
],

Textarea

Official documentation : https://www.advancedcustomfields.com/resources/textarea

[
    'key' => 'field_5981dae27e190',
    'label' => 'test',
    'name' => 'test',
    'type' => 'textarea',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'placeholder' => '',
    'maxlength' => '',
    'rows' => '',
    'new_lines' => '',
],

Number

Official documentation : Not available

[
    'key' => 'field_5981dae87e191',
    'label' => 'test',
    'name' => 'test',
    'type' => 'number',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'placeholder' => '',
    'prepend' => '',
    'append' => '',
    'min' => '',
    'max' => '',
    'step' => '',
],

Email

Official documentation : Not available

[
    'key' => 'field_5981daef7e192',
    'label' => 'test',
    'name' => 'test',
    'type' => 'email',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'placeholder' => '',
    'prepend' => '',
    'append' => '',
],

Url

Official documentation : Not available


[
    'key' => 'field_5981daf67e193',
    'label' => 'test',
    'name' => 'test',
    'type' => 'url',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'placeholder' => '',
],

Password

Official documentation : Not available

[
    'key' => 'field_5981daff7e194',
    'label' => 'test',
    'name' => 'test',
    'type' => 'password',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'placeholder' => '',
    'prepend' => '',
    'append' => '',
],

Content

Wysiwyg

Official documentation : https://www.advancedcustomfields.com/resources/wysiwyg

[
    'key' => 'field_5981db077e195',
    'label' => 'test',
    'name' => 'test',
    'type' => 'wysiwyg',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
    'tabs' => 'all',
    'toolbar' => 'full',
    'media_upload' => 1,
    'delay' => 0,
],

Oembed

Official documentation : https://www.advancedcustomfields.com/resources/oembed

[
    'key' => 'field_5981db107e196',
    'label' => 'test',
    'name' => 'test',
    'type' => 'oembed',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'width' => '',
    'height' => '',
],

Image

Official documentation : https://www.advancedcustomfields.com/resources/image

[
    'key' => 'field_5981db207e198',
    'label' => 'test',
    'name' => 'test',
    'type' => 'image',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'return_format' => 'array',
    'preview_size' => 'thumbnail',
    'library' => 'all',
    'min_width' => '',
    'min_height' => '',
    'min_size' => '',
    'max_width' => '',
    'max_height' => '',
    'max_size' => '',
    'mime_types' => '',
],

File

Official documentation : https://www.advancedcustomfields.com/resources/file

[
    'key' => 'field_5981db297e199',
    'label' => 'test',
    'name' => 'test',
    'type' => 'file',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'return_format' => 'array',
    'library' => 'all',
    'min_size' => '',
    'max_size' => '',
    'mime_types' => '',
],

Official documentation : https://www.advancedcustomfields.com/resources/gallery

[
    'key' => 'field_5981db327e19a',
    'label' => 'test',
    'name' => 'test',
    'type' => 'gallery',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'min' => '',
    'max' => '',
    'insert' => 'append',
    'library' => 'all',
    'min_width' => '',
    'min_height' => '',
    'min_size' => '',
    'max_width' => '',
    'max_height' => '',
    'max_size' => '',
    'mime_types' => '',
],

Choice

Select

Official documentation : https://www.advancedcustomfields.com/resources/select

[
    'key' => 'field_5981db877e1a2',
    'label' => 'test',
    'name' => 'test',
    'type' => 'select',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'choices' => [
    ],
    'default_value' => [
    ],
    'allow_null' => 0,
    'multiple' => 0,
    'ui' => 0,
    'ajax' => 0,
    'return_format' => 'value',
    'placeholder' => '',
],

Checkbox

Official documentation : https://www.advancedcustomfields.com/resources/checkbox

[
    'key' => 'field_5981db9b7e1a3',
    'label' => 'test',
    'name' => 'test',
    'type' => 'checkbox',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'choices' => [
    ],
    'allow_custom' => 0,
    'save_custom' => 0,
    'default_value' => [
    ],
    'layout' => 'vertical',
    'toggle' => 0,
    'return_format' => 'value',
],

Radio

Official documentation : https://www.advancedcustomfields.com/resources/radio-button

[
    'key' => 'field_5981dba57e1a4',
    'label' => 'test',
    'name' => 'test',
    'type' => 'radio',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'choices' => [
    ],
    'allow_null' => 0,
    'other_choice' => 0,
    'save_other_choice' => 0,
    'default_value' => '',
    'layout' => 'vertical',
    'return_format' => 'value',
],

True / false

Official documentation : https://www.advancedcustomfields.com/resources/true-false

[
    'key' => 'field_5981dbb07e1a5',
    'label' => 'test',
    'name' => 'test',
    'type' => 'true_false',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'message' => '',
    'default_value' => 0,
    'ui' => 0,
    'ui_on_text' => '',
    'ui_off_text' => '',
],

Relational

Official documentation : https://www.advancedcustomfields.com/resources/link

[
    'key' => 'field_5981db3c7e19b',
    'label' => 'test',
    'name' => 'test',
    'type' => 'link',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'return_format' => 'array',
],

Post Object

Official documentation : https://www.advancedcustomfields.com/resources/post-object/

[
    'key' => 'field_5981db427e19c',
    'label' => 'test',
    'name' => 'test',
    'type' => 'post_object',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'post_type' => [
    ],
    'taxonomy' => [
    ],
    'allow_null' => 0,
    'multiple' => 0,
    'return_format' => 'object',
    'ui' => 1,
],

Official documentation : https://www.advancedcustomfields.com/resources/page-link

[
    'key' => 'field_5981db497e19d',
    'label' => 'test',
    'name' => 'test',
    'type' => 'page_link',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'post_type' => [
    ],
    'taxonomy' => [
    ],
    'allow_null' => 0,
    'allow_archives' => 1,
    'multiple' => 0,
],

Relationship

Official documentation : https://www.advancedcustomfields.com/resources/relationship

[
    'key' => 'field_5981db527e19e',
    'label' => 'test',
    'name' => 'test',
    'type' => 'relationship',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'post_type' => [
    ],
    'taxonomy' => [
    ],
    'filters' => [
        0 => 'search',
        1 => 'post_type',
        2 => 'taxonomy',
    ],
    'elements' => '',
    'min' => '',
    'max' => '',
    'return_format' => 'object',
],

Taxonomy

Official documentation : https://www.advancedcustomfields.com/resources/taxonomy

[
    'key' => 'field_5981db717e1a0',
    'label' => 'test',
    'name' => 'test',
    'type' => 'taxonomy',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'taxonomy' => 'category',
    'field_type' => 'checkbox',
    'allow_null' => 0,
    'add_term' => 1,
    'save_terms' => 0,
    'load_terms' => 0,
    'return_format' => 'id',
    'multiple' => 0,
],

User

Official documentation : Not available

[
    'key' => 'field_5981db787e1a1',
    'label' => 'test',
    'name' => 'test',
    'type' => 'user',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'role' => '',
    'allow_null' => 0,
    'multiple' => 0,
],

Structure / misc.

Google Map

Official documentation : https://www.advancedcustomfields.com/resources/google-map

[
    'key' => 'field_5981dbba7e1a6',
    'label' => 'test',
    'name' => 'test',
    'type' => 'google_map',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'center_lat' => '',
    'center_lng' => '',
    'zoom' => '',
    'height' => '',
],

In order to use this field, you must add you Google API Key. You might want to replace the GMAP_API_KEY constant with your own key.

/**
 *  Register API Key for ACF Google maps
 */
add_filter('acf/fields/google_map/api', function ($api) {
    $api['key'] = GMAP_API_KEY;
    return $api;
});

you can create a new mu-plugin or put it in your theme’s functions.php

Date picker

Official documentation : https://www.advancedcustomfields.com/resources/date-picker

[
    'key' => 'field_5981dbc27e1a7',
    'label' => 'test',
    'name' => 'test',
    'type' => 'date_picker',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'display_format' => 'd/m/Y',
    'return_format' => 'd/m/Y',
    'first_day' => 1,
],

Date time picker

Official documentation : https://www.advancedcustomfields.com/resources/date-time-picker

[
    'key' => 'field_5981dbcc7e1a8',
    'label' => 'test',
    'name' => 'test',
    'type' => 'date_time_picker',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'display_format' => 'd/m/Y g:i a',
    'return_format' => 'd/m/Y g:i a',
    'first_day' => 1,
],

Time picker

Official documentation : https://www.advancedcustomfields.com/resources/time-picker

[
    'key' => 'field_5981dbd87e1a9',
    'label' => 'test',
    'name' => 'test',
    'type' => 'time_picker',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'display_format' => 'g:i a',
    'return_format' => 'g:i a',
],

Color picker

Official documentation : https://www.advancedcustomfields.com/resources/color-picker

[
    'key' => 'field_5981dbe27e1aa',
    'label' => 'test',
    'name' => 'test',
    'type' => 'color_picker',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'default_value' => '',
],

Message

Official documentation : Not available

[
    'key' => 'field_5981dbeb7e1ab',
    'label' => 'test',
    'name' => '',
    'type' => 'message',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'message' => '',
    'new_lines' => 'wpautop',
    'esc_html' => 0,
],

Tab

Official documentation : https://www.advancedcustomfields.com/resources/tab

[
    'key' => 'field_5981dbf57e1ac',
    'label' => 'test',
    'name' => '',
    'type' => 'tab',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'placement' => 'top',
    'endpoint' => 0,
],

Group

Official documentation : https://www.advancedcustomfields.com/resources/group

[
    'key' => 'field_5981dbfe7e1ad',
    'label' => 'test',
    'name' => 'test',
    'type' => 'group',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'layout' => 'block',
    'sub_fields' => [
    ],
],

Repeater

Official documentation : https://www.advancedcustomfields.com/resources/repeater

[
    'key' => 'field_5981dc0a7e1ae',
    'label' => 'test',
    'name' => 'test',
    'type' => 'repeater',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'collapsed' => '',
    'min' => 0,
    'max' => 0,
    'layout' => 'table',
    'button_label' => '',
    'sub_fields' => [
        [
            'key' => 'field_5981dc147e1af',
            'label' => 'test',
            'name' => 'test',
            'type' => 'flexible_content',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => [
                'width' => '',
                'class' => '',
                'id' => '',
            ],
            'layouts' => [
                '5981dc1bf40ab' => [
                    'key' => '5981dc1bf40ab',
                    'name' => 'test',
                    'label' => 'test',
                    'display' => 'block',
                    'sub_fields' => [
                        [
                            'key' => 'field_5981dc337e1b0',
                            'label' => 'test',
                            'name' => 'test',
                            'type' => 'text',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => [
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ],
                            'default_value' => '',
                            'placeholder' => '',
                            'prepend' => '',
                            'append' => '',
                            'maxlength' => '',
                        ],
                    ],
                    'min' => '',
                    'max' => '',
                ],
            ],
            'button_label' => 'Add Row',
            'min' => '',
            'max' => '',
        ],
    ],
],

Clone

Official documentation : https://www.advancedcustomfields.com/resources/clone

[
    'key' => 'field_5981dc3e7e1b1',
    'label' => 'test',
    'name' => 'test',
    'type' => 'clone',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'wrapper' => [
        'width' => '',
        'class' => '',
        'id' => '',
    ],
    'clone' => '',
    'display' => 'seamless',
    'layout' => 'block',
    'prefix_label' => 0,
    'prefix_name' => 0,
],
Envie d'en parler ?

Ecrivez un email ou DM sur twitter