ACF\AI\GEO

GEO::format_field_value_for_jsonldpublic staticACF 6.8.0

Format ACF field value for JSON-LD output

Shared helper method for formatting field values consistently. Checks for field-type-specific formatting methods in this order:

  1. Pre-filter to allow complete bypass of formatting logic
  2. format_value_for_jsonld() - custom method for JSON-LD formatting (if field type implements it)
  3. Field-type-specific formatting, defaulting to format_value_for_rest() for most types
  4. Post-filter on the final formatted value

Method of the class: GEO{}

Returns

Mixed. Formatted value.

Usage

$result = GEO::format_field_value_for_jsonld( $value, $field_object );
$value(mixed) (required)
The field value.
$field_object(array) (required)
The ACF field object.

Changelog

Since 6.8.0 Introduced.

GEO::format_field_value_for_jsonld() code ACF 6.8.8

public static function format_field_value_for_jsonld( $value, $field_object ) {
	$field_type_name = $field_object['type'] ?? '';
	$field_name      = $field_object['name'] ?? '';

	/**
	 * Filter to bypass the default formatting logic entirely
	 *
	 * Return a non-null value to bypass all default formatting.
	 * This runs before any other formatting logic.
	 *
	 * @param mixed|null $pre_value     Return non-null to bypass default formatting.
	 * @param mixed      $value         The raw field value.
	 * @param array      $field_object  The ACF field object.
	 * @param string     $field_type_name The field type name.
	 */
	$pre_value = apply_filters( 'acf/schema/format_value/pre', null, $value, $field_object, $field_type_name );
	$pre_value = apply_filters( "acf/schema/format_value/pre/type={$field_type_name}", $pre_value, $value, $field_object );
	$pre_value = apply_filters( "acf/schema/format_value/pre/name={$field_name}", $pre_value, $value, $field_object );

	if ( null !== $pre_value ) {
		return $pre_value;
	}

	// Get the field type class instance.
	$field_type = acf_get_field_type( $field_type_name );

	// First priority: Check if field type has a custom format_value_for_jsonld method.
	if ( $field_type && method_exists( $field_type, 'format_value_for_jsonld' ) ) {
		$formatted_value = $field_type->format_value_for_jsonld( $value, null, $field_object );
	} else {
		// Second priority: Use format_value_for_rest or return as-is.
		if ( $field_type && method_exists( $field_type, 'format_value_for_rest' ) ) {
			$formatted_value = $field_type->format_value_for_rest( $value, null, $field_object );
		} else {
			// Final fallback: return value as-is.
			// Arrays are valid JSON-LD (e.g., multi-select values).
			$formatted_value = $value;
		}
	}

	/**
	 * Filter the formatted value before returning
	 *
	 * Allows modification of the value after all default formatting has been applied.
	 *
	 * @param mixed  $formatted_value The formatted value.
	 * @param mixed  $value           The raw field value.
	 * @param array  $field_object    The ACF field object.
	 * @param string $field_type_name The field type name.
	 */
	$formatted_value = apply_filters( 'acf/schema/format_value', $formatted_value, $value, $field_object, $field_type_name );
	$formatted_value = apply_filters( "acf/schema/format_value/type={$field_type_name}", $formatted_value, $value, $field_object );
	$formatted_value = apply_filters( "acf/schema/format_value/name={$field_name}", $formatted_value, $value, $field_object );

	return $formatted_value;
}