A read-only field whose value is derived from other fields at save time.
Field::computed(string $key, \Closure $callback): ComputedFieldThe $callback receives the full sanitized field map and must return the computed value.
->setIndex(bool $index = true): staticWrite the computed value to a separate index meta row (for WP_Query searches).
Computed values are written to the same JSON blob as regular fields, after the sanitization stage.
{ "price": 100, "tax_rate": 20, "total_price": 120 }If ->setIndex(true) is set, the value is also written to _ctrlfield_idx_total_price.
Field::number('price')->label('Price (€)'),
Field::number('tax_rate')->label('Tax Rate (%)'),
Field::computed('total_price', function (array $fields): float {
return ($fields['price'] ?? 0) * (1 + ($fields['tax_rate'] ?? 0) / 100);
})->setIndex(true),Full name from parts:
Field::text('first_name')->label('First Name'),
Field::text('last_name')->label('Last Name'),
Field::computed('full_name', function (array $fields): string {
return trim(($fields['first_name'] ?? '') . ' ' . ($fields['last_name'] ?? ''));
})->setIndex(true),Computed fields are read exactly like regular fields:
{{ ctrlfield_get('total_price') }} {{-- 120 --}}
{{ ctrlfield_get('full_name') }} {{-- "Alice Smith" --}}- The callback fires after
SanitizationStage, so$fieldscontains clean, coerced values. - Computed fields are never shown in the admin UI (they have no input).
- If the callback throws, the pipeline raises a
PipelineExceptionand the save is aborted. - The callback is not called at read time — only on save. The stored value is returned as-is.