跳转到主要内容

acf/schema/format_value

Filter a field value after default formatting is applied.

Description

This filter allows you to modify field values after ACF has applied its default JSON-LD formatting. Use this for post-processing values or applying custom transformations.

For pre-processing before default formatting, use acf/schema/format_value/pre.

Changelog

  • Added in version 6.8.0

Parameters

apply_filters( 'acf/schema/format_value', $formatted_value, $value, $field_object, $field_type );
  • $formatted_value (mixed) The value after default formatting
  • $value (mixed) The original raw field value
  • $field_object (array) The field object with settings
  • $field_type (string) The field type name

Return

(mixed) The modified formatted value.

Example

This example ensures prices always have 2 decimal places.

/**
 * Format prices with 2 decimal places.
 *
 * @param mixed  $formatted_value The formatted value.
 * @param mixed  $value           The raw value.
 * @param array  $field_object    The field object.
 * @param string $field_type      The field type.
 * @return mixed Modified value.
 */
function my_acf_schema_format_prices( $formatted_value, $value, $field_object, $field_type ) {
    $property = $field_object['schema_property'] ?? '';

    if ( $property === 'Offer.price' || $property === 'price' ) {
        return number_format( (float) $formatted_value, 2, '.', '' );
    }

    return $formatted_value;
}
add_filter( 'acf/schema/format_value', 'my_acf_schema_format_prices', 10, 4 );

This example converts plain text URLs to proper link objects.

/**
 * Convert URLs to WebPage objects.
 *
 * @param mixed  $formatted_value The formatted value.
 * @param mixed  $value           The raw value.
 * @param array  $field_object    The field object.
 * @param string $field_type      The field type.
 * @return mixed Modified value.
 */
function my_acf_schema_format_urls( $formatted_value, $value, $field_object, $field_type ) {
    $property = $field_object['schema_property'] ?? '';

    // Convert mainEntityOfPage to WebPage object
    if ( $property === 'mainEntityOfPage' && is_string( $formatted_value ) ) {
        return array(
            '@type' => 'WebPage',
            '@id'   => $formatted_value,
        );
    }

    return $formatted_value;
}
add_filter( 'acf/schema/format_value', 'my_acf_schema_format_urls', 10, 4 );