acf/schema/format_value/pre
Filter a field value before default formatting is applied.
Description
This filter allows you to modify or replace field values before ACF applies its default JSON-LD formatting. Return a non-null value to bypass the default formatting entirely.
Changelog
- Added in version 6.8.0
Parameters
apply_filters( 'acf/schema/format_value/pre', $pre_value, $value, $field_object, $field_type );
$pre_value(mixed) Pre-formatted value, or null to use default formatting$value(mixed) The raw field value$field_object(array) The field object with settings$field_type(string) The field type name
Return
(mixed|null) The pre-formatted value, or null to use default formatting.
Example
This example provides custom duration formatting.
/**
* Custom ISO 8601 duration formatting.
*
* @param mixed $pre_value Pre-formatted value or null.
* @param mixed $value The raw value.
* @param array $field_object The field object.
* @param string $field_type The field type.
* @return mixed Pre-formatted value or null.
*/
function my_acf_schema_duration_format( $pre_value, $value, $field_object, $field_type ) {
$property = $field_object['schema_property'] ?? '';
// Handle duration properties
$duration_properties = array( 'prepTime', 'cookTime', 'totalTime', 'duration' );
if ( in_array( $property, $duration_properties, true ) && is_numeric( $value ) ) {
$hours = floor( $value / 60 );
$minutes = $value % 60;
if ( $hours > 0 && $minutes > 0 ) {
return sprintf( 'PT%dH%dM', $hours, $minutes );
} elseif ( $hours > 0 ) {
return sprintf( 'PT%dH', $hours );
} else {
return sprintf( 'PT%dM', $minutes );
}
}
return $pre_value; // Use default formatting
}
add_filter( 'acf/schema/format_value/pre', 'my_acf_schema_duration_format', 10, 4 );
This example handles custom date formatting.
/**
* Custom date formatting for events.
*
* @param mixed $pre_value Pre-formatted value or null.
* @param mixed $value The raw value.
* @param array $field_object The field object.
* @param string $field_type The field type.
* @return mixed Pre-formatted value or null.
*/
function my_acf_schema_event_dates( $pre_value, $value, $field_object, $field_type ) {
$property = $field_object['schema_property'] ?? '';
// Format event dates with timezone
if ( in_array( $property, array( 'startDate', 'endDate' ), true ) && $value ) {
$timestamp = strtotime( $value );
if ( $timestamp ) {
return date( 'c', $timestamp ); // ISO 8601 with timezone
}
}
return $pre_value;
}
add_filter( 'acf/schema/format_value/pre', 'my_acf_schema_event_dates', 10, 4 );
这篇文档对你有帮助吗?
谢谢你的反馈!