Guide

Unit Control

PHP Field Configuration Reference

Unit

Use this config when fieldType is unit.

1) Base Parameters

ParameterRequiredTypeDefaultChoicesDescription
fieldTypeYesstringunitunitSets 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) Unit Specific Parameters

ParameterRequiredTypeDefaultChoicesDescription
sizeNostringdefaultdefault, smallAdjusts the size of the input.
disableUnitsNoboolfalsetrue, falseIf true, the unit selector is hidden.
isPressEnterToChangeNoboolfalsetrue, falseIf true, the ENTER key press is required to trigger an onChange. A change is also triggered on blur.
isResetValueOnUnitChangeNoboolfalsetrue, falseIf true, and the selected unit provides a default value, this value is set when changing units.
isUnitSelectTabbableNobooltruetrue, falseDetermines if the unit select is tabbable.

3) PHP Array Schema

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

[
    'fieldType' => 'unit',
    'name' => 'post_unit',
    'fieldLabel' => 'Post Unit',
    'required' => false,
    'disabled' => false,
    'hideLabelFromVision' => false,
    'fieldHelpText' => 'Enter a value with unit.',
    'className' => 'custom-class',
    'fieldLabelPosition' => 'top',
    'fieldLabelTextTransform' => 'uppercase',
    'size' => 'default',
    'disableUnits' => false,
    'isPressEnterToChange' => false,
    'isResetValueOnUnitChange' => false,
    'isUnitSelectTabbable' => true,
]

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'       => 'design_settings',
        'meta_box_title'    => 'Design Settings',
        'meta_box_context'  => 'side',
        'meta_box_priority' => 'default',
        'fields'            => [
            [
            'fieldType' => 'unit',
            'name' => 'post_unit',
            'fieldLabel' => 'Post Unit',
            'required' => false,
            'disabled' => false,
            'hideLabelFromVision' => false,
            'fieldHelpText' => 'Enter a value with unit.',
            'className' => 'custom-class',
            'fieldLabelPosition' => 'top',
            'fieldLabelTextTransform' => 'uppercase',
            'size' => 'default',
            'disableUnits' => false,
            'isPressEnterToChange' => false,
            'isResetValueOnUnitChange' => false,
            'isUnitSelectTabbable' => true,
            ],
        ],
    ];

    return $configs;
} );

4) Stored Value Type

Field TypeMeta Value Type
unitstring (value with unit, for example "10px")

Was this helpful?