acf/prepare_field


目录

Description

Used to modify the $field settings or to prevent the field from being rendered within a form.

This filter is similar to acf/load_field, only it is applied later in the lifecycle when the field is about to be rendered. By this time, the field’s value has also been loaded.

Changelog

Parameters

apply_filters( 'acf/prepare_field', $field );

Modifers

This filter provides modifiers to target specific fields. The following filter names are available:

Examples

Modifying a field

This example demonstrates how to modify a specific field’s settings before it is rendered.

functions.php

<?php
function my_acf_prepare_field( $field ) {

    // Lock-in the value "Example".
    if( $field['value'] === 'Example' ) {
        $field['readonly'] = true;
    };
    return $field;
}

// Apply to all fields.
// add_filter('acf/prepare_field', 'my_acf_prepare_field');

// Apply to select fields.
// add_filter('acf/prepare_field/type=select', 'my_acf_prepare_field');

// Apply to fields named "example_field".
add_filter('acf/prepare_field/name=example_field', 'my_acf_prepare_field');

// Apply to field with key "field_123abcf".
// add_filter('acf/prepare_field/key=field_123abcf', 'my_acf_prepare_field');

Preventing a field

This example demonstrates how to prevent a field from being rendered.

functions.php

<?php
function my_acf_prepare_field( $field ) {

    // Don't show this field once it contains a value.
    if( $field['value'] ) {
        return false;
    }
    return $field;
}

// Apply to fields named "example_field".
add_filter('acf/prepare_field/name=example_field', 'my_acf_prepare_field');

版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC