跳转到主要内容

acf/schema/format_value/pre/name={field_name}

Pre-filter values for a specific field name before default formatting.

Description

This filter is a field name specific version of acf/schema/format_value/pre. It allows you to pre-process values before ACF’s default formatting, but only for a specific field name.

The dynamic portion of the hook name, {field_name}, refers to the ACF field name.

Changelog

  • Added in version 6.8.0

Parameters

apply_filters( 'acf/schema/format_value/pre/name={field_name}', $pre_value, $value, $field_object );
  • $pre_value (mixed) Pre-formatted value from previous filters, or null
  • $value (mixed) The raw field value
  • $field_object (array) The field object with settings

Return

(mixed|null) The pre-formatted value, or null to use default formatting.

Example

This example provides custom formatting for a specific event date field.

/**
 * Custom date formatting for event_date field.
 *
 * @param mixed $pre_value    Pre-formatted value or null.
 * @param mixed $value        The raw value (date string).
 * @param array $field_object The field object.
 * @return mixed Pre-formatted value or null.
 */
function my_acf_schema_format_event_date( $pre_value, $value, $field_object ) {
    if ( ! $value ) {
        return $pre_value;
    }

    // Convert to ISO 8601 with timezone
    $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $value );
    if ( ! $date ) {
        return $pre_value;
    }

    // Set timezone from WordPress settings
    $timezone = wp_timezone();
    $date->setTimezone( $timezone );

    return $date->format( 'c' );
}
add_filter( 'acf/schema/format_value/pre/name=event_date', 'my_acf_schema_format_event_date', 10, 3 );

This example enriches a product SKU field with additional data.

/**
 * Enrich product_sku field with full product data.
 *
 * @param mixed $pre_value    Pre-formatted value or null.
 * @param mixed $value        The raw SKU value.
 * @param array $field_object The field object.
 * @return mixed Pre-formatted value or null.
 */
function my_acf_schema_format_product_sku( $pre_value, $value, $field_object ) {
    if ( ! $value ) {
        return $pre_value;
    }

    // Look up product by SKU in external system
    $product = my_get_product_by_sku( $value );
    if ( ! $product ) {
        return $value; // Return SKU as-is
    }

    return array(
        '@type'       => 'Product',
        'sku'         => $value,
        'name'        => $product->name,
        'description' => $product->description,
        'offers'      => array(
            '@type'         => 'Offer',
            'price'         => $product->price,
            'priceCurrency' => 'USD',
        ),
    );
}
add_filter( 'acf/schema/format_value/pre/name=product_sku', 'my_acf_schema_format_product_sku', 10, 3 );