Guide

Font Size Control

PHP Field Configuration Reference

Font Size

Use this config when fieldType is font_size.

1) Base Parameters

ParameterRequiredTypeDefaultChoicesDescription
fieldTypeYesstringfont_sizefont_sizeSets 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) Font Size Specific Parameters

ParameterRequiredTypeDefaultChoicesDescription
sizeNostringdefaultdefault, __unstable-largeSets control size.
unitsNostring["px","em","rem","vw","vh"]px, em, rem, vw, vhAvailable units for custom font size selection. Accepts a JSON array of unit strings.
disableCustomFontSizesNoboolfalsetrue, falseIf true, the user is forced to pick one of the pre-defined font sizes from fontSizes.
withResetNobooltruetrue, falseIf true, a reset button will be displayed alongside the input field when a custom font size is active.
withSliderNoboolfalsetrue, falseIf true, a slider will be displayed alongside the input field when a custom font size is active.
fallbackFontSizeNointIf no value exists, defines the starting position for the font size picker slider. Only relevant if withSlider is true.
valueModeNostringliteral, slugDetermines how the value prop should be interpreted. literal: contains the actual font size value. slug: contains the slug of the selected font size.
fontSizesNostringJSON array of font size objects. Example: [{"name": "Small", "size": 12, "slug": "small"}]

3) PHP Array Schema

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

[
    'fieldType' => 'font_size',
    'name' => 'post_font_size',
    'fieldLabel' => 'Post Font Size',
    'required' => false,
    'disabled' => false,
    'hideLabelFromVision' => false,
    'fieldHelpText' => 'Set the font size.',
    'className' => 'custom-class',
    'fieldLabelPosition' => 'top',
    'fieldLabelTextTransform' => 'uppercase',
    'size' => 'default',
    'units' => '["px","em","rem","vw","vh"]',
    'disableCustomFontSizes' => false,
    'withReset' => true,
    'withSlider' => false,
    'fontSizes' => '[{"name": "Small", "size": 12, "slug": "small"}, {"name": "Normal", "size": 16, "slug": "normal"}]',
]

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' => 'font_size',
            'name' => 'post_font_size',
            'fieldLabel' => 'Post Font Size',
            'required' => false,
            'disabled' => false,
            'hideLabelFromVision' => false,
            'fieldHelpText' => 'Set the font size.',
            'className' => 'custom-class',
            'fieldLabelPosition' => 'top',
            'fieldLabelTextTransform' => 'uppercase',
            'size' => 'default',
            'units' => '["px","em","rem","vw","vh"]',
            'disableCustomFontSizes' => false,
            'withReset' => true,
            'withSlider' => false,
            'fontSizes' => '[{"name": "Small", "size": 12, "slug": "small"}, {"name": "Normal", "size": 16, "slug": "normal"}]',
            ],
        ],
    ];

    return $configs;
} );

4) Stored Value Type

Field TypeMeta Value Type
font_sizestring (font size value with unit, for example "16px")

Was this helpful?