Guide

Token Field Control

PHP Field Configuration Reference

Token Field

Use this config when fieldType is token_field.

1) Base Parameters

ParameterRequiredTypeDefaultChoicesDescription
fieldTypeYesstringtoken_fieldtoken_fieldSets control type.
nameYesstringSets control name. Use snake_case.
fieldLabelYesstringSets control label.
defaultNostringSets control default value.
requiredNoboolfalsetrue, falseSets control required.
disabledNoboolfalsetrue, falseSets control disabled.
hideLabelFromVisionNoboolfalsetrue, falseSets control hide label from vision.
fieldHelpTextNostringSets control help text.
classNameNostringSets control css class name.
fieldLabelPositionNostringtoptop, leftSets control label position.
fieldLabelTextTransformNostringuppercaseuppercase, capitalize, lowercaseSets control label text transform.

2) Token Field Specific Parameters

ParameterRequiredTypeDefaultChoicesDescription
maxLengthNointDisables adding new tokens once the number of tokens reaches or exceeds this value.
maxSuggestionsNoint100The maximum number of suggestions to display at a time.
suggestionsNostringAn array of suggested token strings. Separate values with commas (e.g. "Africa, Europe, Asia").
allowOnlySuggestionsNoboolfalsetrue, falseIf true, only tokens matching a suggestion can be added. Only relevant if suggestions is set.
__experimentalAutoSelectFirstMatchNoboolfalsetrue, falseIf true, automatically selects the first matching suggestion when Enter (or space if tokenizeOnSpace) is pressed.
__experimentalExpandOnFocusNoboolfalsetrue, falseIf true, the suggestions list is always expanded when the input field has focus.
isBorderlessNoboolfalsetrue, falseWhen true, renders tokens without a background.
tokenizeOnBlurNoboolfalsetrue, falseIf true, adds any incomplete token value as a new token when the field loses focus.
tokenizeOnSpaceNoboolfalsetrue, falseIf true, adds a token when the field is focused and space is pressed.

3) PHP Array Schema

Here is an example of how to use the token field control in a post meta configuration:

[
    'fieldType' => 'token_field',
    'name' => 'post_token_field',
    'fieldLabel' => 'Post Token Field',
    'required' => false,
    'disabled' => false,
    'hideLabelFromVision' => false,
    'fieldHelpText' => 'Add tags or keywords.',
    'className' => 'custom-class',
    'fieldLabelPosition' => 'top',
    'fieldLabelTextTransform' => 'uppercase',
    'maxSuggestions' => 100,
    'suggestions' => 'Africa, Europe, Asia',
    'allowOnlySuggestions' => false,
    'isBorderless' => false,
    'tokenizeOnBlur' => false,
    'tokenizeOnSpace' => false,
]

3) Hook-Based Example (Post Meta Config)

It is available to use this control in post meta fields, term meta fields and user settings page and options page fields.

Available hooks:

  • native_custom_fields_post_meta_fields
  • native_custom_fields_term_meta_fields
  • native_custom_fields_user_meta_fields
  • native_custom_fields_options_page_fields
add_filter( 'native_custom_fields_post_meta_fields', function( array $configs ): array {
    $post_type = 'book';

    if ( ! isset( $configs[ $post_type ] ) || ! is_array( $configs[ $post_type ] ) ) {
        $configs[ $post_type ] = [
            'post_type' => $post_type,
            'sections'  => [],
        ];
    }

    $configs[ $post_type ]['sections'][] = [
        'meta_box_id'       => 'post_options',
        'meta_box_title'    => 'Post Options',
        'meta_box_context'  => 'side',
        'meta_box_priority' => 'default',
        'fields'            => [
            [
            'fieldType' => 'token_field',
            'name' => 'post_token_field',
            'fieldLabel' => 'Post Token Field',
            'required' => false,
            'disabled' => false,
            'hideLabelFromVision' => false,
            'fieldHelpText' => 'Add tags or keywords.',
            'className' => 'custom-class',
            'fieldLabelPosition' => 'top',
            'fieldLabelTextTransform' => 'uppercase',
            'maxSuggestions' => 100,
            'suggestions' => 'Africa, Europe, Asia',
            'allowOnlySuggestions' => false,
            'isBorderless' => false,
            'tokenizeOnBlur' => false,
            'tokenizeOnSpace' => false,
            ],
        ],
    ];

    return $configs;
} );

4) Stored Value Type

Field TypeMeta Value Type
token_fieldstring (serialized JSON array of token strings, for example '["tag1","tag2","tag3"]')

Was this helpful?